9 iPhone Memory Management Links and Resources
Memory management is the process of keeping track of objects in your applications and “freeing” objects that you no longer want to keep around (otherwise nasty memory leaks can occur). On the Mac, you can optionally allow Objective C to deal with the memory management on your behalf, but as an iPhone developer, it’s a necessary chore – Apple considers automatic memory management to be too performance intensive.
We’ve collected together several of the most useful iPhone memory management related resources we could find and linked them below. They’re not all iPhone specific and they range from ultra simple to reasonably complex.. so dive in:
Memory Management from Wikipedia – What is memory management? If you’re asking yourself that question (despite the explanation above) check out this solid introduction to the general concepts from Wikipedia.
Practical Memory Management by Apple – Direct from Apple’s official Cocoa documentation comes this guide that “provides a practical perspective on memory management.” It’s basically a solid “cheat sheet” to memory management. A very no-nonsense introduction to memory management in Objective C at the most basic level. It’s particularly useful, too, because it includes examples of poor memory management.
Guide to Objective C Memory Management by John Muchow – This is the most practical, code driven and comfortable walkthrough I’ve seen so far on memory management with Objective C. Very comprehensive and easy going.
Memory Management with Objective C / Cocoa / iPhone by Mehmet S. Akten – Less of a walkthrough than Muchow’s article, and more of a general overview of the concepts involved. There’s a lot to go through here and it’s presented in an easy to read fashion. If you’re an existing C / C++ programmer, it’s worth reading for the comparison between those languages and Objective C.
iPhone Memory Management – A Brief Introduction by Mr. X – A very short explanation of memory management on the iPhone with the most basic of code examples. Not bad as a refresher. It sums up one of the main concepts very succinctly:
The rule of managing memory is to make sure that, by the time the program has finished executing, the number of “ownership methods” called on an object will equal the number of “loss-of-ownership” methods.
Memory Management with Cocoa/WebObjects by Manu Iyengar – This article is ten years old (1999!) but I’ve seen it cited as a particularly good introduction to memory management in Objective C. It certainly reads well and because it predates Objective C 2.0 it assumes you’ll be doing all the memory management (no optional garbage collection back then!) It shows you how to do object reference counting, how to deal with temporary objects, and goes into some “gory” details on how autorelease works.
Custom Classes PDF from the Stanford Cocoa Programming course – The custom classes PDF from Stanford’s Cocoa programming course briefly covers memory management in Objective C generally. One slide in particular (excerpted above) covers the main ground and then a couple of examples are shown. If you want a no-nonsense introduction to the most basic memory management example in Objective C possible, this is the one for you. The section in question starts about half way through the document (but it’s all worth reading).
Memory Management with Collections from the MacRumors forums – This isn’t an article, but a forum thread where a question is posed about memory management and an interesting discussion (with code) takes place. Only follow this one if you’re exhaustively reading about the topic – it’s interesting, but it’s not a must-read.
NSZombieEnabled – A run through of NSZombieEnabled, an environment variable that makes the runtime use “zombie” objects – these are objects that step in the place of deallocated objects and any messages sent to these objects (which should never occur if your code is “good”) will be logged. This can help you track down nasty deallocation issues.
Got any other good resources, cheat sheets, links, or similar? Post them in the comments – we’ll certainly check them out!