我正在尝试执行相当大的 CoreData 导入(大约 25,000 行),同时仍然保持相当低的内存占用。我已经阅读了有关有效导入数据的文档,并努力实现那里建议的所有内容(包括将我的 MOC 的 undoManager 设置为 nil)。
不幸的是,在运行以下代码时,我的应用程序内存使用量仍然攀升至 180MB 左右。完成后,无论最终的 NSAutoreleasePool 耗尽调用如何,应用程序都将位于 180MB 左右。
通过 Allocations 运行应用程序显示 95% 的内存使用可归因于我的[self.moc save:&error]
调用。我在这里做错了什么?
- (void)generateCache
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUInteger count = 0, batchSize = 1000;
// SNIP SNIP
// Iterate over our directory structure
for(NSString *item in directoryStructure)
{
NSDictionary *info = [fm attributesOfItemAtPath:item error:nil];
FileRecord *record = (FileRecord *)[NSEntityDescription insertNewObjectForEntityForName:@"FileRecord" inManagedObjectContext:self.moc];
record.size = [NSNumber numberWithUnsignedLongLong:[info fileSize]];
record.path = item;
count ++;
if(count == batchSize)
{
NSError *error = nil;
if([self.moc save:&error])
{
NSLog(@"MOC saved down and reset");
[self.moc reset];
[pool drain];
pool = [[NSAutoreleasePool alloc] init];
count = 0;
}
}
}
// Perform any necessary last minute MOC saves
if (count != 0) {
[self.moc save:nil];
[self.moc reset];
}
// Drain our NSAutoreleasePool
[pool drain];
// Tell our main thread that we're done
if ([self respondsToSelector:@selector(completedCache)])
{
[self performSelectorOnMainThread:@selector(completedCache) withObject:nil waitUntilDone:NO];
}
}