iOS 中的核心数据内存占用空间不断增长

core data memory foot print in iOS keeps growing

我正在尝试备份 Core Data SQLite 数据库。此代码成功处理了 运行ning 数据库并合并了 WAL 文件。不幸的是,每次 运行s 我都会看到我的内存占用量增加了大约 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" }};
}

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

由于重复次数太少,我将其写为 "answer"(即使它可能不是 解决方案):我认为这是一个很好的做法关于 "cache_size" 作为另一个 NSSQLitePragmasOption 并相应地限制它:

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

参见 www.sqlite.org 声明

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

因此,您可能希望在丢弃 NSManagedObjectContext 的任何实例之前调用 reset。这将强制导致所有活动对象出现故障,从而打破任何保留周期(除非您自然地再次访问这些对象)。 dealloc 自动提示 reset 的文档中没有明确说明,因此它似乎不是合同保证。

我看到的所有问题都与 Xcode 计划直接相关。 产品->方案 Select Run-Options UNCHECK -> 队列调试(启用回溯记录)

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