释放分配以响应内存不足警告,但应用程序仍然崩溃
我正在构建一个应用程序,用于查看从 API 中获取的照片。每张照片的大小约为 1MB。我设置了一个“幻灯片”来显示一张照片,然后转到下一张照片,就像用户实际使用该应用程序一样。我正在 Instruments 中的 iPad 1 上进行测试。
当我的应用程序收到内存不足警告时,我将转储当前未向用户显示的所有照片,以及从 API 返回的所有缓存模型数据。我发现仪器中的分配显着下降,虚拟内存使用也有类似的下降。即使消耗的内存有所下降,我的应用程序仍然被操作系统杀死。
应用程序响应 2-3 个内存警告,在终止前不会崩溃。
我最近切换到 ARC,所以也许有一些我不明白的地方?我认为将引用设置为零就足够了。这是我的内存模型转储图像数据的代码:
[[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
有谁有知道为什么我的应用程序在释放内存的情况下仍被杀死吗?
I'm building an app for viewing photos I pull down from an API. Each photo is ~1MB in size. I've set up a "slideshow" to show a photo, then move onto the next one, like a user would actually use the app. I'm testing on an iPad 1 in Instruments.
When my app receives a low memory warning, I'm dumping all photos that are currently not being displayed to the user, as well as all cached model data returned from the API. I'm seeing a significant drop in my allocations in Instruments, and a similar drop in the virtual memory use. Even with this drop in consumed memory, my app is still being killed by the OS.
The application responds to 2-3 memory warnings without crashing before being terminated.
I've recently switched to ARC, so maybe there's something I'm not understanding? I assume setting my references to nil is sufficient. Here's my code for the in-memory models dumping their image data:
[[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;
}];
Which is getting called. I also have an NSMutableDictionary which I'm calling removeAllObjects on when I received the low memory warning. I'm getting the following in the device console:
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
Does anyone have any idea why my app is being killed even though it's freeing memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这只是将指针设置为nil,而不是释放实际的对象。释放对象,然后它们将被释放(如果它们的保留计数达到 0)。
由于您使用的是 ARC,因此只需将属性设置为 nil 即可。
This is just setting the pointers to
nil
, not releasing the actual objects. Release the objects, then they'll get deallocated (if their retain count hits 0).Since you're using ARC, just set the properties to nil.
事实证明,我在其他地方保留了对模型类的引用 - 即使它们在内存警告期间释放了图像数据,它们也没有被释放。最终,它们太多了,应用程序崩溃了。
Turns out I was hanging onto references to the model classes somewhere else - they weren't getting dealloc'd, even if they released their image data during memory warnings. Eventually there were too many of them and the app crashed.