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

我最近遇到了一个问题。 我正在使用一个图像caching库,它运作良好,但最终我遇到内存问题,应用程序只是退出本身(我想这是因为它只是内存不足)。 从图像caching库中读取源代码之后,我发现当有内存警告事件时,它会释放caching的所有图像(图像很大)。 有没有反正我手动和直接发送内存警告事件的设备? 我正在使用xcode仪器工具来评估内存使用情况。

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

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); 

内存警告可以通过调用UIApplication的私有方法来产生。 它在iOS 6.1及以下版本可以正常工作

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

注意:在将应用程序提交给iTunes之前,请先移除该select器,否则将被拒绝。