In any program you write, you must ensure that you manage resources effectively and efficiently. One such resource is your program’s memory. In an Objective-C program, you must make sure that objects you create are disposed of when you no longer need them.
In a complex system, it could be difficult to determine exactly when you no longer need an object. Cocoa defines some rules and principles that help making that determination easier.
Important: In Mac OS X v10.5 and later, you can use automatic memory management by adopting garbage collection. This is described in Garbage Collection Programming Guide. Garbage collection is not available on iPhone.
You should read this document to learn about the object ownership policies and related techniques for creating, copying, retaining, and disposing of objects in a reference-counted environment.
This document does not describe details of allocating and initializing objects, and implementing initializer methods. These tasks are discussed in Allocating and Initializing Objects in The Objective-C Programming Language.
This document contains the following articles:
“Memory Management Rules” summarizes the rules for object ownership and disposal.
“Object Ownership and Disposal” describes the primary object-ownership policy.
“Practical Memory Management” gives a practical perspective on memory management.
“Autorelease Pools” describes the use of autorelease pools—a mechanism for deferred deallocation—in Cocoa programs.
“Accessor Methods” describes how to implement accessor methods.
“Implementing Object Copy” discusses issues related to object copying, such as deciding whether to implement a deep or shallow copy and approaches for implementing object copy in your subclasses.
“Memory Management of Core Foundation Objects in Cocoa” gives guidelines and techniques for memory management of Core Foundation objects in Cocoa code.
“Memory Management of Nib Objects” discusses memory management issues related to nib files.
© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-10-21)