Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Memory Management in iOS App Development

0aa5743bd364213c11abd871b2325f65?s=47 Sumin Byeon
September 03, 2009

Memory Management in iOS App Development

0aa5743bd364213c11abd871b2325f65?s=128

Sumin Byeon

September 03, 2009
Tweet

More Decks by Sumin Byeon

Other Decks in Programming

Transcript

  1. Memory Management for iPhone Application Development Sumin Byeon (sumin@cs.arizona.edu)

  2. Advanced Topic

  3. Significance Limited memory (128MB, 256MB) No garbage collection No virtual

    memory User experience
  4. Related Functions alloc copy retain release autorelease

  5. Alloc Allocates memory for an object, and returns it with

    retain count of 1[1]. Almost equivalent to the new keyword in C+ +, Java, etc. [1] http:/ /developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html
  6. Copy Makes a copy of an object, and return retain

    count of 1.
  7. Master Theorem #1 If you allocated it, you should release

    it. If you didn’t explicitly allocate it, don’t release it.
  8. Example ! NSString *str = [[NSString alloc] initWithCString:"Boom baby"]; !

    NSLog(str); ! [str release];
  9. Example ! NSString *str = [NSString stringWithCString:"Boom baby"]; ! NSLog(str);

  10. Retain Increases the retain count of an object by 1.

  11. Release Decreases the retain count of an object by 1.

  12. Master Theorem #2 If an object is being retained by

    something else, you may release it immediately. (e.g. [UIView addSubview:view])
  13. Master Theorem #3 Don’t you dare call dealloc. Always call

    release, and dealloc will be called when the retain count becomes zero.
  14. Autorelease

  15. Autorelease with Threads

  16. Instruments Charles Magahern (zanneth@zanneth.com)

  17. Questions?