14

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.

bryanmac
  • 38,941
  • 11
  • 91
  • 99
Kevin Xue
  • 756
  • 6
  • 8

2 Answers2

15

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);
bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • http://stackoverflow.com/questions/2784892/simulate-memory-warnings-from-the-code-possible Mentions an undocumented API as well – tapi Mar 29 '12 at 13:48
12

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

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

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

Zubair
  • 5,833
  • 3
  • 27
  • 49