释放分配以响应低内存警告,但应用程序仍然崩溃

时间:2011-10-05 23:52:59

标签: objective-c ios memory-management automatic-ref-counting didreceivememorywarning

我正在构建一个用于查看我从API下载的照片的应用。每张照片大小约为1MB。我已经设置了一个“幻灯片”来显示照片,然后转到下一个,就像用户实际使用该应用程序一样。我正在使用仪器的iPad 1进行测试。

当我的应用收到内存不足警告时,我会将当前未显示的所有照片转储给用户,以及从API返回的所有缓存模型数据。我看到我在仪器中的分配显着下降,虚拟内存的使用也有类似的下降。即使消耗内存减少,我的应用程序仍被操作系统杀死。

应用程序在终止之前响应2-3个内存警告而不会崩溃。

我最近换了ARC,所以也许有些东西我不理解?我假设设置我对nil的引用就足够了。这是我的内存模型转储图像数据的代码:

[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
    NSLog(@"Received memory warning; clear image for photo named \"%@\"", _name);
        _image = nil;
        _imageThumbnail = nil;
}];

哪个被调用。我还有一个NSMutableDictionary,当我收到低内存警告时,我正在调用removeAllObjects。我在设备控制台中获得以下内容:

Oct  5 19:43:46 unknown configd[25] <Notice>: jetsam: kernel termination snapshot being created
Oct  5 19:43:46 unknown com.apple.launchd[1] <Notice>: (com.apple.accessoryd) Exited: Killed: 9
Oct  5 19:43:46 unknown com.apple.launchd[1] <Notice>: (com.apple.locationd) Exited: Killed: 9
Oct  5 19:43:46 unknown com.apple.launchd[1] <Notice>: (com.apple.mediaserverd) Exited: Killed: 9
Oct  5 19:43:46 unknown com.apple.launchd[1] <Notice>: (UIKitApplication:com.500px[0xd492]) Exited: Killed: 9
Oct  5 19:43:47 unknown kernel[0] <Debug>: launchd[1996] Builtin profile: accessoryd (sandbox)
Oct  5 19:43:47 unknown ReportCrash[1999] <Error>: libMobileGestalt loadBasebandMobileEquipmentInfo: CommCenter error: 1:45
Oct  5 19:43:47 unknown ReportCrash[1999] <Error>: libMobileGestalt copyInternationalMobileEquipmentIdentity: Could not get mobile equipment info dictionary
Oct  5 19:43:47 unknown ReportCrash[1999] <Error>: Saved crashreport to /Library/Logs/CrashReporter/LowMemory-2011-10-05-194347.plist using uid: 0 gid: 0, synthetic_euid: 0 egid: 0
Oct  5 19:43:47 unknown DTMobileIS[1655] <Warning>: _memoryNotification : <NSThread: 0x1cd31410>{name = (null), num = 1}
Oct  5 19:43:47 unknown DTMobileIS[1655] <Warning>: _memoryNotification : {
        OSMemoryNotificationLevel = 0;
        timestamp = "2011-10-05 23:43:47 +0000";
    }
Oct  5 19:43:47 unknown DTMobileIS[1655] <Warning>: _memoryNotification : <NSThread: 0x1cd31410>{name = (null), num = 1}
Oct  5 19:43:47 unknown DTMobileIS[1655] <Warning>: _memoryNotification : {
        OSMemoryNotificationLevel = 0;
        timestamp = "2011-10-05 23:43:47 +0000";
    }
Oct  5 19:43:48 unknown com.apple.locationd[1997] <Notice>: locationd was started after an unclean shutdown
Oct  5 19:43:49 unknown SpringBoard[29] <Warning>: Application '500px' exited abnormally with signal 9: Killed: 9

This is my allocations/VM instrument up until the crash.

有没有人知道为什么我的应用程序被杀死了,即使它正在释放内存?

2 个答案:

答案 0 :(得分:3)

    _image = nil;
    _imageThumbnail = nil;

这只是将指针设置为nil,而不是释放实际对象。释放对象,然后它们将被释放(如果它们的保留计数达到0)。

由于您使用ARC,只需将属性设置为nil。

答案 1 :(得分:1)

原来我在其他地方的模型类的引用上 - 他们没有得到dealloc'd,即使他们在内存警告期间发布了他们的图像数据。最终他们有太多而应用程序崩溃了。