NSGarbage​Collector provides a convenient interface to the garbage collection system.


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 collect​If​Needed and collect​Exhaustively).


Shared Instance

+ default​Collector

Returns the default garbage collector.

Collection State

- disable

Temporarily disables collections.

- enable

Enables collection after collection has been disabled.

- is​Enabled

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

- is​Collecting

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

Triggering Collection

- collect​Exhaustively

Tells the receiver to collect iteratively.

- collect​If​Needed

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

Manipulating External References

- disable​Collector​For​Pointer:​​

Specifies that a given pointer will not be collected.

- enable​Collector​For​Pointer:​​

Specifies that a given pointer may be collected.

Accessing an Unscanned Memory Zone

- zone

Returns a zone of unscanned memory.


Inherits From