Who Should Read This Document
Organization of This Document
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.
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.
Note: If you are starting a new project targeted at Mac OS X v10.5 and later, you should typically use garbage collection unless you have good reason to use the techniques described here.
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 2.0 Programming Language.
This document contains the following articles:
“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.
“Using Memory Zones” discusses the use of memory zones.
“Memory Management of Nib Objects” discusses memory management issues related to nib files.
“Memory Management Rules” summarizes the rules for object ownership and disposal.
© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-05-06)