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

发布于 2024-12-08 13:02:03 字数 2931 浏览 1 评论 0原文

我正在构建一个应用程序,用于查看从 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

This is my Allocations/VM Instrument up until the crash.

有谁有知道为什么我的应用程序在释放内存的情况下仍被杀死吗?

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

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

Does anyone have any idea why my app is being killed even though it's freeing memory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫漫岁月 2024-12-15 13:02:03
    _image = nil;
    _imageThumbnail = nil;

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

由于您使用的是 ARC,因此只需将属性设置为 nil 即可。

    _image = nil;
    _imageThumbnail = 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.

小清晰的声音 2024-12-15 13:02:03

事实证明,我在其他地方保留了对模型类的引用 - 即使它们在内存警告期间释放了图像数据,它们也没有被释放。最终,它们太多了,应用程序崩溃了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文