3

我正在尝试执行相当大的 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];
    }
}
4

1 回答 1

1

与其处理自动释放池,不如通过使用NSManagedObject's创建托管对象来明确管理托管对象的生命周期initWithEntity:insertIntoManagedObjectContext:?您可以在修改对象的属性后安全地释放它们,因为托管对象上下文会保留新插入的对象——直到它保存到持久存储中。

另外,我应该提到我在您的代码中看到的几个问题:

  1. 正如上面提到的那样,您没有记录save:操作中的错误。你真的应该 - 这可能会突出一些(可能不相关的)问题。

  2. 如果 save: 成功,你真的不需要调用reset. 请参阅核心数据指南中的此部分

于 2011-06-09T21:42:13.830 回答