Memory management for the iPhone Os


iPhone OS memory management

The iPhone OS doesn't support garbage collection because of power considerations. That means every object that's created must eventually have its memory released by hand - at least, if you don't want to introduce a memory leak into your program. The fundamental rule of memory management in the iPhone OS is this: if you allocate memory for an object, you must release it. This is done via the release message:

 [object release];

Send that message when you're all done using an object, and you've done your proper duty as a programmer. Note that we said you only must release the memory if you allocated the memory for it. You don't allocate the memory for those, which means you're not responsible for releasing it. Instead, the class object that does the creation has to clean up its memory. How does the OS know when you've finished working with the object it created for you? That's done through the wonders of autorelease.

Autorelease alternative

If you're responsible for the creation of an object and you're going to pass it off to some other class for usage, you should autorelease the object before you send it off. This is done with the autorelease method:

 [object autorelease];

You'll typically send the autorelease message just before you return the object at the end of a method. After an object has been autoreleased, it's watched over by a special NSAutoreleasePool. The object is kept alive for the scope of the method to which it's been passed, and then the NSAutoreleasePool cleans it up.

Retaining and counting

What if you want to hold onto an object that has been passed to you and that will be autoreleased? In that case, you send it a retain message:

 [object retain];

When you do this, you're saying you want the object to stay around, but now you've become responsible for its memory as well: you must send a release message at some point to balance your retain. At this point, we should probably back up and explain the underlying way that the iPhone OS manages memory objects. It does so by maintaining a count of object usage. By default, it's set to 1. Each retain message increases that count by 1, and each release message reduces that count by 1. When the count drops to 0, the memory for the object is freed up. Therefore, all memory management can be thought of as pairs of messages. If you balance every alloc and every retain with a release, your object will eventually be freed up when you're done with it.

Legal Disclaimer

Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.


This article was sent to us by: Christian Glousten at 09232010

Related Articles

1. Market segmentation is a necessity when selling an iPhone iPad app
Segmentation starts when you post your app on the App Store through the categorization required by the store. Some developers are not sure what category their app should be...

2. Building Your Apps Total Message
Building Your App’s Total Message Some people think marketing your iPhone/iPad app consists of doing a little advertising and a press release. Actually, mar...

3. An effective iPhone iPad app name can help increase your sales
Choose an Effective App Name An effective iPhone/iPad app name can help increase your sales because the buyer does not have to invest as much effort to understand...

4. Your App Store text to help you captivate your readers attention
App Store Text: Lighten It Up In this mobile economy, people are reading less when it comes to their communications. Attention spans are short and, with the crush...

5. Your product website for App Store product page
Build a Simple, Clean Product Website Your product website should be similar in look and feel to your App Store product page. A carryover between the two sites wi...

6. Strive for Immediate Positive Reviews for your Ipad app
Strive for Immediate Positive Reviews There is nothing like getting a good reference. Whether you realize it, we all tend to listen to what other people say about...

7. Gets a positive review by an iPhone iPad app review site
Positive External Reviews Sometimes a developer is lucky in that his or her app gets a positive review by an iPhone/iPad app review site. That review gets picked ...