Technical Q&A QA1599

Determining if an application uses Objective-C Garbage Collection

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

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

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


DateNotes
2008-09-08

New document that shows how to determine if an application or executable is using Objective-C Garbage Collection.