Try the search, it's linked to some great forums

Sunday, June 9, 2013

Memory Management

This post contains a series of notes, links and tips on memory management in Objective-C.

Reference links
* Great intro to memory management - http://maniacdev.com/2009/07/video-tutorials-on-objective-c-memory-management
* Apple's Memory Usage Guidelines - https://developer.apple.com/library/mac/#documentation/Performance/Conceptual/ManagingMemory/Articles/FindingLeaks.html
* A truly great Instruments Tutorial - http://www.raywenderlich.com/23037/how-to-use-instruments-in-xcode


Notes
* retainCount is a property of NSObject that is used by the system to manage an object's memory.  It is essentially the count of other objects that have a reference to that object.
   [anObject retainCount] returns an integer retain count for anObject
   [anObject retain] increases the count by 1
   [anObject release] decreases the count by 1
   When the count = 0 the system deallocates the memory for that object
* Rule #1 - alloc, new & copy create a new object with retainCount = 1.  Whenever you create an object with one of these methods, your are responsible for releasing that object.
* autoRelease is a method that adds your object to the autoRelease pool where it is released at the end of a program cycle.  It simplifies your task of having to release your objects.
* Memory leaks are blocks of allocated memory that the program no longer references. Leaks waste space by filling up pages of memory with inaccessible data and waste time due to extra paging activity. Leaked memory eventually forces the system to allocate additional virtual memory pages for the application, the allocation of which could have been avoided by reclaiming the leaked memory. (by Apple's Guidelines above)
* For apps that use malloc, memory leaks are bugs and should always be fixed. For apps that use only Objective-C objects, the compiler’s ARC feature deallocates objects for you and generally avoids the problem of memory leaks. However, apps that mix the use of Objective-C objects and C-based structures must manage the ownership of objects more directly to ensure that the objects are not leaked. (by Apple's Guidelines above)



Tips
  • unbounded memory growth - This happens where memory continues to be allocated and is never given a chance to be deallocated.  Use heap shot analysis to troubleshoot (see rayWenderlich tutorial above)
  •