views:

98

answers:

2

For any application that I have on my Mac, is there a way to tell if it was compiled with GC enabled, or if it's doing manual memory management?

+5  A: 

Within the mach-o is a flag used to determine if a binary compiled with GC support, for non-GC, or mixed mode.

I don't know of anything that queries these bits via a more friendly API.

The markgc.c source within the Objective-C runtime can read said flags. You could refactor it to your needs, as desired.

Kind of curious why you need to know?

bbum
otool maybe can?
kch
as for why, see my comment on the question after Chris's.
kch
I don't think otool spews the needed info. Maybe so, though -- you'll have to parse the output.
bbum
oh but it does. see my answer.
kch
Ooh... nice. I'd forgotten that was added.
bbum
+3  A: 

I found the answer here. Mind you that the original post is wrong, but contains a comment by Mark Rowe, an Apple engineer, that points the way.

I have re-run the otool commands he mentions on my machine with the current OS (10.6.4). Here's the output:

$ uname -a
Darwin meaningless.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386

### Mail doesn't use GC
$ otool -oV /Applications/Mail.app/Contents/MacOS/Mail | tail -3
Contents of (__DATA,__objc_imageinfo) section
  version 0
    flags 0x0

### Xcode supports GC and retain/release
$ otool -oV /Developer/Applications/Xcode.app/Contents/MacOS/Xcode | tail -3
Contents of (__DATA,__objc_imageinfo) section
  version 0
    flags 0x2 OBJC_IMAGE_SUPPORTS_GC

Mark Rowe's explanation:

The field of interest here is the “flags” field of the __image_info section of the __OBJC segment. If garbage collection is supported it will have the value 0×2 and will be shown as “GC RR” to represent that both garbage collection and retain/release are supported. If garbage collection is required then the field will have the value 0×4 and will be shown as “GC-only” indicating that only garbage collection is supported and that retain/release is not available. The field can also contain other values, but those two are the only values that are relevant to garbage collection.

kch