Easy Guide For Proper Objective-C Memory Management

Memory management can be tricky in Objective-C.

If you don’t follow some solid principles for memory management it’s easy to get lazy, and then you can quickly find yourself getting stuck for hours checking every retain and release statement.

Even worse is if you’re going through someone else’s code, and they messed things up. Going through to fix memory management in someone else’s code can take forever.

A great tool that I’ve found for making this easier is Clang which works well with an interface such as this one: Clang GUI For Mac OS X.   You can also use SHIFT-CMD-A (build & analyze) in Xcode to bring up clang alerts.  Clang will notify you of any reference count problems, and syntactical mistakes that could lead to messed up memory management.  Even with Clang you can still run into problems.

Fortunately, Brent Simmon has written a great easy to follow guide on some simple principles to follow to alleviate memory problems before they start: How I Manage Memory.  It’s a good read and will help you to eliminate problems before they start.

[Source: Brent Simmon]
[Via: @rwenderlich]

DeliciousTwitterTechnoratiFacebookLinkedInEmail

Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can send a Tweet to @maniacdev on Twitter or submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.

  • http://www.Vellios.com Nick Vellios

    I’m curious, what does this tool do that simply pressing Cmd-Ctrl-A (Build & Analyze) in Xcode does not do?

  • http://www.Vellios.com Nick Vellios

    I’m curious, what does this tool do that simply pressing Cmd-Ctrl-A (Build & Analyze) in Xcode does not do?

  • http://www.Vellios.com Nick Vellios

    I’m curious, what does this tool do that simply pressing Cmd-Ctrl-A (Build & Analyze) in Xcode does not do?

  • John

    I’d forgotten to include shift-cmd-a can also be used. That’s what I get for typing something up then coming back to it later. You can set it up to print out a snazzy report, but yes those are the same alerts build and analyze.

  • John

    I’d forgotten to include shift-cmd-a can also be used. That’s what I get for typing something up then coming back to it later. You can set it up to print out a snazzy report, but yes those are the same alerts build and analyze.

  • John

    I’d forgotten to include shift-cmd-a can also be used. That’s what I get for typing something up then coming back to it later. You can set it up to print out a snazzy report, but yes those are the same alerts build and analyze.

  • Chris

    “Avoid autorelease when possible” … “Use autorelease as much as possible” …

    No wonder everyone gets confused.

    Are there no guides anywhere that can give good solid advice instead of “This is how I do it…”?

  • Chris

    “Avoid autorelease when possible” … “Use autorelease as much as possible” …

    No wonder everyone gets confused.

    Are there no guides anywhere that can give good solid advice instead of “This is how I do it…”?

  • Chris

    “Avoid autorelease when possible” … “Use autorelease as much as possible” …

    No wonder everyone gets confused.

    Are there no guides anywhere that can give good solid advice instead of “This is how I do it…”?

  • John

    Well, that guide is pretty good.

    If performance is not an issue you may as well use autorelease frequently. On the original iPhone memory was such a big issue you really needed to micromanage things.

    With more recent editions though that is not nearly as much of an issue.

  • John

    Well, that guide is pretty good.

    If performance is not an issue you may as well use autorelease frequently. On the original iPhone memory was such a big issue you really needed to micromanage things.

    With more recent editions though that is not nearly as much of an issue.

  • http://www.Vellios.com Nick Vellios

    Optimizing for phones is ALWAYS an issue. If people start thinking more available memory means sloppier coding practices are acceptable we will soon have the Windows Vista Effect on the iPhone.

  • http://www.Vellios.com Nick Vellios

    Optimizing for phones is ALWAYS an issue. If people start thinking more available memory means sloppier coding practices are acceptable we will soon have the Windows Vista Effect on the iPhone.

  • http://kamleshwar.com/blog/ Kamleshwar

    Right, Avoid whenever possible to use Autorelease as it will remain in memory until pool is not released. It can become de facto memory leaks.

    Good effort really informative. Keep Posting.

    Thanks
    Kamleshwar