使用iCloud的核心数据导致低内存警告和崩溃,内存使用量不断增长

时间:2014-03-16 19:57:39

标签: ios core-data memory out-of-memory icloud

我正在开发一个小型的业余爱好应用程序并且遇到了一个奇怪的内存问题,这个问题只有在我使用iCloud的Core Data时才会发生。如果我将应用程序切换为使用本地存储,则随着时间的推移,内存使用情况会很好(通常为17-18Mb)。

当我将应用程序与iCloud一起使用时,应用程序会不断使用越来越多的内存(每秒增加大约4-5Mb)直到它崩溃。

我已经对乐器进行了一些分析,但无法弄清楚为什么会发生这种情况。有没有人对如何开始寻找解决方案有任何想法?

这是一个截图,显示了应用程序使用了多少内存以及增长是如何线性的。应用此屏幕截图后应用程序崩溃了几秒钟。

Xcode memory usage

以下是Core Data堆栈设置中的一些代码。

    NSString *iCloudEnabledAppID = @"iCloudData";
    NSString *dataFileName = @"Moviedo.sqlite";
    NSString *iCloudDataDirectoryName = @"Data.nosync";
    NSString *iCloudLogsDirectoryName = @"Logs";
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];
    NSPersistentStoreCoordinator *psc = self.managedObjectContext.persistentStoreCoordinator;
    NSString *iCloudData = [[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName] stringByAppendingPathComponent:dataFileName];
    NSURL *iCloudLogsPath = [NSURL fileURLWithPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]];
    NSURL *storeURL = [NSURL fileURLWithPath:iCloudData];

    NSDictionary *options = @{
                              NSMigratePersistentStoresAutomaticallyOption: @(YES),
                              NSInferMappingModelAutomaticallyOption: @(YES),
                              NSPersistentStoreUbiquitousContentNameKey: iCloudEnabledAppID,
                              NSPersistentStoreUbiquitousContentURLKey: iCloudLogsPath
                              };

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        if (iCloud) {

            DDLogInfo(@"iCloud is working");

            if([fileManager fileExistsAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]] == NO) {
                NSError *fileSystemError;
                [fileManager createDirectoryAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]
                       withIntermediateDirectories:YES
                                        attributes:nil
                                             error:&fileSystemError];
                if(fileSystemError != nil) {
                    NSLog(@"Error creating database directory %@", fileSystemError);
                }
            }

            DDLogVerbose(@"iCloudData = %@", iCloudData);

            NSError* error;
            if (! [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
                DDLogError(@"Error creating iCloud data store: %@",error);
                abort();
            }
        }
        else {
            [self setupLocalStorage];
        }
    });

以下是Instruments的一些截图。我让应用运行了大约2分钟。最终内存使用量为453Mb。请注意,我手动停止录制,应用程序没有崩溃。

当我打开前4个符号名称中的3个(按字节用法排序)时,调用树中似乎还有很多PFUbiquity*个调用。再一次,我不确定我能用这些信息做些什么。

Allocation Summary

Inverted Call Tree

0 个答案:

没有答案