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

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

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

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

应用程序在终止前响应 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.

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

最佳答案

    _image = nil;
    _imageThumbnail = nil;

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

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

关于objective-c - 响应低内存警告释放分配,但应用程序仍然崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669071/

相关文章:

c++ - 在 C++ 中将内存分配给 char*

ios - 当不再显示 UIViewController 时如何继续 CAKeyframeAnimation?

objective-c - UIWebView 自动调整字体大小以最适合页面

objective-c - 禁用重新排列标签 iphone

iOS webbrowser会自动关闭websocket连接吗?

ios - UITableView - 初始加载时未着色的单元格?

ios - 在 Xcode 5.1.1 中开发的 iPhone 应用程序可以在什么最低版本上运行

c++ - `std::string` 分配是我当前的瓶颈 - 如何使用自定义分配器进行优化?

c++ - 如果我在管理 C++ 对象内存的 Objective C 中混合 C++ 代码,ARC 会处理它吗?

c++ - 堆上的多线程(取消)分配