Simulated memory warnings on your iOS device

I like to make sure my apps can handle memory warnings appropriately. There's a command to simulate a memory warning in the Simulator, but I also like to test this functionality on a real device. The best way is to let it happen naturally; you can create favorable conditions for a memory warning by running other apps, playing music with another app in the background, or by allocating memory within your app and holding onto it. The problem is that it's hard to test every view within your app this way.

I now add the following lines to my AppDelegate.m file to simulate periodic warnings:

This sets up a timer that fires every 20 seconds and simulates a memory warning using an undocumented function. Because it's undocumented, it's a sure-fire way to get your app rejected; be sure to remove these lines when you're done testing.

This helps me find bugs in my didReceiveMemoryWarning and viewDidUnload methods. As I navigate within my app, lazy-loaded resources and views of previous controllers are continually unloaded; if I missed something I'll likely see a crash.