有没有办法手动向iPhone设备发送内存警告?

时间:2011-12-07 05:40:52

标签: iphone ios xcode cocoa-touch memory-warning

这些天我遇到了一个问题。我正在使用图像缓存库,它运行良好,但最终我遇到了内存问题,应用程序只是自行退出(我想这是因为它只是耗尽了内存)。从图像缓存库中读取源代码后,我发现当有内存警告事件时,它会释放所有缓存的图像(图像很大)。无论如何,我是否手动直接向设备发送内存警告事件?我正在使用xcode仪器工具来评估内存使用情况。

2 个答案:

答案 0 :(得分:14)

您可以在模拟器中手动模拟:

Hardware -> Simulate Memory Warning

您也可以通过编程方式进行模拟:

- (void)simulateMemoryWarning
{
#if TARGET_IPHONE_SIMULATOR
  #ifdef DEBUG
    CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),    (CFStringRef)@"UISimulatedMemoryWarningNotification", NULL, NULL, true);
  #endif
#endif
}

CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)@"UISimulatedMemoryWarningNotification", NULL, NULL, true);

答案 1 :(得分:14)

通过调用UIApplication的私有方法可以生成内存警告。它适用于iOS 6.1及以下版本

  [[UIApplication sharedApplication]performSelector:@selector(_performMemoryWarning)];

注意:在将应用程序提交到iTunes之前删除该选择器调用,否则将被拒绝。