2015-05-25 34 views
2

我想备份一个核心数据SQLite数据库。此代码成功处理正在运行的数据库并合并WAL文件。不幸的是,每次运行时,我都会在内存占用空间中看到大约3-5 MB的凹凸。程序运行一段时间后,这会导致问题。有人可以帮我回收内存吗?我认为将所有东西都设置为零将从RAM中释放所有的对象,但似乎并不是这样。在iOS中的核心数据内存足迹不断增加

-(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点之后出现这种情况不会影响内存占用的一切。

回答

1

我见过的所有问题都直接对Xcode Scheme负责。 产品展示 - >方案 选择运行选项取消选中 - >队列调试(启用原路返回录音)

一旦这样做,所有的内存占用增长的立即消失。

+0

哇,我不知道你是怎么想出来的,但是这似乎也解决了我的问题。内存占用量将无限增长(超过1GB),现在可以轻松生活在25MB。 –

0

由于太少代表我在写这作为一个“答案”(即使它可能不是溶液):我认为这是考虑“CACHE_SIZE”良好做法作为另一个NSSQLitePragmasOption并限制其因此:

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

看到www.sqlite.org的声明

0

我认为这很可能是因为在核心数据对象图的所有关系都是强引用。所以你们都保证保留周期。对于进一步的诊断,您应该使用仪器的泄漏工具并查看哪种类型的物体泄漏。

因此,在丢弃它们之前,您可能希望在创建NSManagedObjectContext的任何实例上调用reset。这将强行断开所有活动对象,从而破坏任何保留周期(除非您自然再次访问对象)。 dealloc自动提示reset并不明确在文档中,因此它似乎不是合同保证。