Search code examples
iphoneiosmemoryios6didreceivememorywarning

When is didReceiveMemoryWarning actually called


I've never personally seen didReceiveMemoryWarning called before. Is there a known threshold of memory usage where it gets called?


Solution

  • It's called when the system has low memory, then you need to purge your UIViewController.

    You can simulate a memory warning in the Simulator: Hardware > Simulate Memory Warning and see what happens.

    If you are supporting prior versions to iOS6 it's important to take note of the following from Apple's documentation:

    Prior to iOS 6, when a low-memory warning occurred, the UIViewController class purged its views if it knew it could reload or recreate them again later. If this happens, it also calls the viewWillUnload and viewDidUnload methods to give your code a chance to relinquish ownership of any objects that are associated with your view hierarchy, including objects loaded from the nib file, objects created in your viewDidLoad method, and objects created lazily at runtime and added to the view hierarchy. On iOS 6, views are never purged and these methods are never called. If your view controller needs to perform specific tasks when memory is low, it should override the didReceiveMemoryWarning method.

    About the threshold, I try not to exceed 12MB of memory.