【问题标题】:core data memory foot print in iOS keeps growingiOS 中的核心数据内存占用量不断增长
【发布时间】:2015-08-07 08:52:45
【问题描述】:

我正在尝试备份 Core Data SQLite 数据库。此代码成功处理正在运行的数据库并合并 WAL 文件。不幸的是,每次运行时,我都会看到我的内存占用增加了大约 3-5 MB。这会在程序运行一段时间后引起问题。有人可以帮我恢复记忆吗?我认为将所有内容设置为 nil 会释放 RAM 中的所有对象,但似乎并非如此。

-(void) backupDatabaseWithThisTimeStamp: (int) timeStamp withCompletionBlock:(void (^)(void))completion {
    NSDate *backupDate = [NSDate date];
    NSError *error;

    [self.defaultPrivateQueueContext save:&error];
    if (error) {
        NSLog(@"error -> %@",error);
    }
    dispatch_async(self.backupQueue, ^{
        // Let's use the existing PSC
        NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

        // Open the store
        id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self persistentStoreURL] options:nil error:nil];

        if (!sourceStore) {
            NSLog(@" failed to add store");
            migrationPSC = nil;
        } else {
            NSLog(@" Successfully added store to migrate");

            NSError *error;
            NSLog(@" About to migrate the store...");
            id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[self backupStoreURLwithTimeStamp: timeStamp] options:[self localStoreOptions] withType:NSSQLiteStoreType error:&error];

            if (migrationSuccess) {
                NSLog(@"store successfully backed up");
                // Now reset the backup preference
                NSManagedObjectContext *tempContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
                tempContext.persistentStoreCoordinator = migrationPSC;
                tempContext.undoManager = nil;

                // clip out data
                [CDrawColorData purgeDataOlderThan:backupDate fromContext:tempContext];

                migrationPSC = nil;
                tempContext = nil;
            }
            else {
                NSLog(@"Failed to backup store: %@, %@", error, error.userInfo);
                migrationPSC = nil;
            }
        }
        migrationPSC = nil;
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) {
                completion();
            }
        });
    });
}

self.backupQueue = _backupQueue = dispatch_queue_create("backup.queue", DISPATCH_QUEUE_SERIAL);

localStoreOptions =
- (NSDictionary*)localStoreOptions {
    return @{NSMigratePersistentStoresAutomaticallyOption:@YES,
         NSInferMappingModelAutomaticallyOption:@YES,
         NSSQLitePragmasOption:@{ @"journal_mode" : @"DELETE" }};
}

注释掉迁移成功点之后发生的所有事情不会影响内存占用。

【问题讨论】:

    标签: ios core-data memory-leaks


    【解决方案1】:

    我看到的所有问题都直接与 Xcode 方案有关。 产品->方案 选择运行选项 UNCHECK -> 队列调试(启用回溯记录)

    完成此操作后,所有内存占用增长立即消失。

    【讨论】:

    • 哇,我不知道你是怎么想出来的,但这似乎也解决了我的问题。内存占用量将无限增长(超过 1GB),现在 25MB 可以舒适地使用。
    【解决方案2】:

    由于代表太少,我将其写为“答案”(即使它可能不是 解决方案):我认为将“cache_size”视为另一个 NSSQLitePragmasOption 是一种好习惯并相应地限制它:

    NSSQLitePragmasOption:@{ @"journal_mode" : @"DELETE", @"cache_size" : @"50"}

    请参阅www.sqlite.org 了解声明

    【讨论】:

      【解决方案3】:

      我认为这很可能是因为 Core Data 对象图中的所有关系都是强引用。所以你几乎可以保证保留周期。如需进一步诊断,您应该使用 Instruments 的泄漏工具并查看泄漏的对象类型。

      因此,您可能希望在您创建的任何NSManagedObjectContext 实例上调用reset,然后再丢弃它们。这将强制对所有活动对象进行故障处理,从而中断任何保留周期(除非您自然地再次访问这些对象)。文档中没有明确说明 dealloc 会自动提示 reset,因此它似乎不是合同保证。

      【讨论】:

        猜你喜欢
        • 2013-10-08
        • 2013-10-20
        • 2016-02-24
        • 1970-01-01
        • 2017-11-09
        • 1970-01-01
        • 1970-01-01
        • 2014-02-25
        • 2015-02-23
        相关资源
        最近更新 更多