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

Kev*_*Xue 14 iphone xcode cocoa-touch ios memory-warning

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

bry*_*mac 14

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

Hardware -> Simulate Memory Warning
Run Code Online (Sandbox Code Playgroud)

您还可以通过编程方式模拟它:

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

CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)@"UISimulatedMemoryWarningNotification", NULL, NULL, true);
Run Code Online (Sandbox Code Playgroud)


Zub*_*air 14

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

  [[UIApplication sharedApplication]performSelector:@selector(_performMemoryWarning)];
Run Code Online (Sandbox Code Playgroud)

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