06-07-2012, 11:13 AM
Garbage Collection Programming Guide
GarbageCollection.pdf (Size: 576.63 KB / Downloads: 50)
Introduction to Garbage Collection
In Mac OS X version 10.5, the Cocoa programming environment is enhanced with automatic memory
management—commonly known as "garbage collection." The "traditional" system of memory management
(using retain, release, and autorelease pools)—herein referred to as “reference counted”—coexists both for
binary compatibility with existing programs as well as for those that choose to not make use of the new
facility. Garbage collection is hence an opt-in system.
These documents describe the complete garbage collection system provided for Cocoa, the functionality
provided, and some of the issues that arise if you adopt this technology.
Who Should Read This Document?
If you are developing applications using Cocoa, you should read at least “Garbage Collection for Cocoa
Essentials” (page 11) to gain an understanding of the garbage collection system. It is strongly recommended
that you also read “Adopting Garbage Collection” (page 17) and “Implementing a finalize Method” (page
31). You are expected to already understand the Objective-C language (see The Objective-C Programming
Language) and to have some familiarity with Cocoa.
Organization of This Document
The following articles explain the problems the garbage collection system addresses, the solutions it provides,
its basic functionality, and common tasks you might perform:
● “Garbage Collection for Cocoa Essentials” (page 11) describes the essential details of the garbage
collection system for Cocoa. At a minimum, you should read this article.
● “Adopting Garbage Collection” (page 17) describes issues related to adopting garbage collection.
● “Architecture” (page 19) describes the design goals and architecture of the technology, and the benefits
you get from using it.
● “Using Garbage Collection” (page 25) describes some of the features you can take advantage of when
you use garbage collection, and some of subtleties you need to be aware of.
● “Implementing a finalize Method” (page 31) describes how to correctly implement a finalize method.
● “Inapplicable Patterns” (page 35) describes Cocoa programming patterns that are not applicable to
garbage collection.
● “Using Core Foundation with Garbage Collection” (page 39) describes how to use Core Foundation
objects with garbage collection.
● “Garbage Collection API” (page 45) provides a summary of API used in garbage collection.
Who Should Read This Document? 9
2010-08-27 | © 2010 Apple Inc. All Rights Reserved.
Introduction to Garbage Collection
See Also
The following documents provide information about related aspects of Cocoa and the Objective-C language.
● The Objective-C Programming Language describes object-oriented programming and describes the
Objective-C programming language.
● Objective-C Runtime Reference describes the data structures and functions of the Objective-C runtime
support library.
● Advanced Memory Management Programming Guide addresses Cocoa's object-ownership policy for
manual memory management and related techniques for creating, copying, retaining, and disposing of
objects.
● Garbage Collection Release Notes provides information about the current release of the technology.
How the Garbage Collector Works
The garbage collector's goal is to form a set of reachable objects that constitute the "valid" objects in your
application, and then to discard any others. When a collection is initiated, the collector initializes the set with
all well-known root objects. The collector then recursively follows strong references from these objects to
other objects, and adds these to the set. At the end of the process, all objects that are not reachable through
a chain of strong references to objects in the root set are designated as "garbage." At the end of the collection
sequence, the unreachable objects are finalized and immediately afterwards the memory they occupy is
recovered.
Root Set and Reference Types
The initial root set of objects is comprised of global variables, stack variables, and objects with external
references (for more details about globals, see “Global Object Pointers” (page 27)). These objects are never
considered as garbage. The root set is comprised of all objects reachable from root objects and all possible
references found by examining the call stacks of every Cocoa thread.