Retired Document
Important: This document may not represent best practices for current development. Links to downloads and other resources may no longer be valid.
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
Date | Notes |
---|---|
2008-09-08 | New document that shows how to determine if an application or executable is using Objective-C Garbage Collection. |
Copyright © 2008 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2008-09-08