Who Should Read This Document
Organization of This Document
Memory management, especially as it concerns Objective-C programs, is an important subject. Some of the more common problems encountered by novice application developers derive from poor memory management. Cocoa provides mechanisms and a policy to assist you in the proper creation, retention, and disposal of objects.
Memory management in Cocoa is largely object-oriented. This programming topic addresses the object-ownership policy and related techniques for creating, copying, retaining, and disposing of objects using “managed memory”.
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.
The material in this programming topic relates to Objective-C objects and is of interest primarily to Objective-C programmers. 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 contains the following articles:
“Object Ownership and Disposal” describes the primary policy.
“Allocating and Initializing Objects” explains techniques for allocating and initializing Objective-C objects.
“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 Rules” summarizes the rules for object ownership and disposal.
Additional information about memory management and nib files can be found in Resource Programming Guide > Nib Files and Cocoa > “The Nib Object Life Cycle.”
Last updated: 2008-02-08