One thing to keep in mind when developing for iPhone is that your game’s available RAMis limited. Typically, iPhone OS uses about 40MB of RAM. This leaves you with less than24MB of RAM available when running your game on an iPhone 2G or 3G. The situationwith iPod touch models is a bit better. Because the iPod touch doesn’t need to receivephone calls or text messages, the operating system uses less RAM than on an iPhone.You have about 32MB of RAM available when running your game on an iPod touch.
The important thing to remember is that your game will be forcibly shut down by theoperating system if it uses too much of the available RAM. If the operating system killsyour app, it's too late to start worrying about why it happened. It is likely you have amemory leak somewhere, and you will need to spend some time debugging the problem.
Cocoa Touch does try to help, though. There is a message received by the app delegatethat lets you try to free up memory before it's too late. The message calls a methodnamed applicationDidReceiveMemoryWarning:. You should use theapplicationDidReceiveMemoryWarning: method to release any cached data yourapplication may hold in the app delegate.
For example, if your game uses data from a web service, you can cache that data thefirst time you retrieve it from the web service. Subsequent uses of the web service datacan access the cache, rather than hitting the web service every time. The app delegateis an ideal place to hold this data, and the applicationDidReceiveMemoryWarning:method should release this cache when memory is low. If your app needs the webservice data again, it can reload it from the web service and cache it until memory is lowagain.
View controllers also receive a similar message, with call to the method namedviewDidUnload. Over the course of a game, you are likely to load several views, one foreach screen in your game. As you navigate from one screen to the other, it is likely theother screens don’t need to take up RAM while they are not visible. Cocoa Touch callsthe viewDidUnload method after a view is removed from the screen. If your viewallocates any objects in memory, it should release them in the viewDidUnload method.