• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - iOS 中的核心数据内存占用量不断增长

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:21:30 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试备份 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap