OGeek|极客世界-中国程序员成长平台

标题: ios - iOS 中的核心数据内存占用量不断增长 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:21
标题: ios - iOS 中的核心数据内存占用量不断增长

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

-(void) backupDatabaseWithThisTimeStamp: (int) timeStamp withCompletionBlockvoid (^)(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 @{NSMigratePersistentStoresAutomaticallyOptionYES,
         NSInferMappingModelAutomaticallyOptionYES,
         NSSQLitePragmasOption{ @"journal_mode" : @"DELETE" }};
}

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



Best Answer-推荐答案


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

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

关于ios - iOS 中的核心数据内存占用量不断增长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30445717/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4