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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8410901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:41:38  来源:igfitidea点击:

Is there a way to send Memory Warning to iPhone device manually?

iphoneiosxcodecocoa-touchmemory-warning

提问by Kevin Xue

I got one problem these days. I'm using an image-cache library, it works well but eventually i met memory issue and the app just quit itself (I guess it's because it just runs out of memory). After read the source code from the image-cache library, i found it's said that when there's memory warning event, it would release all images cached (the images are huge). Is there anyway for me to send Memory warning event to the device manually and directly ? I'm using xcode instrument tool to evaluate the memory usage.

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

采纳答案by bryanmac

You can manually simulate in the simulator:

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

Hardware -> Simulate Memory Warning

You can also simulate it programmatically:

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

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

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

回答by Zubair

Memory warning can be produced by calling an private method of UIApplication. It works fine on iOS 6.1 and below

可以通过调用 UIApplication 的私有方法来产生内存警告。它在 iOS 6.1 及更低版本上运行良好

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

NOTE: Remove that selector call before submitting app to iTunes, otherwise it will be rejected.

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