A convenient interface to the garbage collection system.


@interface NSGarbageCollector : NSObject


Cocoa’s garbage collector is a conservative generational garbage collector. It uses “write-barriers” to detect cross generational stores of pointers so that “young” objects can be collected quickly.

You enable garbage collection (GC) by using the -fobjc-gc compiler option. This switch causes the generation of the write-barrier assignment primitives. You must use this option on your main application file and all others used by the application, including frameworks and bundles. Bundles are ignored if they are not GC-capable.

The collector determines what is garbage by recursively examining all nodes starting with globals, possible nodes referenced from the thread stacks, and all nodes marked as having “external” references. Nodes not reached by this search are deemed garbage. Weak references to garbage nodes are then cleared.

Garbage nodes that are objects are sent (in an arbitrary order) a finalize message, and after all finalize messages have been sent their memory is recovered. It is a runtime error (referred to as “resurrection”) to store a object being finalized into one that is not. For more details, see Implementing a finalize Method in Garbage Collection Programming Guide.

You can request collection from any thread (see collectIfNeeded and collectExhaustively).


Shared Instance

+ defaultCollector

Returns the default garbage collector.

Collection State

- disable

Temporarily disables collections.

- enable

Enables collection after collection has been disabled.

- isEnabled

Returns a Boolean value that indicates whether garbage collection is currently enabled for the current process.

- isCollecting

Returns a Boolean value that indicates whether a collection is currently in progress.

Triggering Collection

- collectExhaustively

Tells the receiver to collect iteratively.

- collectIfNeeded

Tells the receiver to collect if memory consumption thresholds have been exceeded.

Manipulating External References

- disableCollectorForPointer:

Specifies that a given pointer will not be collected.

- enableCollectorForPointer:

Specifies that a given pointer may be collected.

Accessing an Unscanned Memory Zone

- zone

Returns a zone of unscanned memory.


Inherits From

See Also



Allocates collectable memory.


Reallocates collectable memory.


Makes a newly allocated Core Foundation object eligible for collection.

Memory Allocation Options

Constants used to control behavior when allocating or reallocating collectible memory.