Determining if an application uses Objective-C Garbage Collection

Q: How can I tell if an Objective-C application has enabled Garbage Collection?

A: To programmatically test your application to see if garbage collection is enabled, check the class NSGarbageCollector for the presence of a defaultCollector. See Listing 1.

Listing 1: Programmatically determining if Garbage Collection is being used

 if ([NSGarbageCollector defaultCollector] != nil) { 
        /* the Garbage Collector is on */ 
    } else {
        /* retain/release/autorelease/dealloc are being utilized */
    }

It may also be useful during debugging to set the environment variable OBJC_PRINT_GC=YES. When set to YES, this not only tells if garbage collection is on or off, but will dump the state of the collector for each Objective-C image. Look specifically for "GC: is ON" or "GC: is OFF".

For More Information

See also Introduction to Garbage Collection and Garbage Collection API.

Document Revision History

Date Notes
2008-09-08 First Version

Posted: 2008-09-08


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.