How can I determine if a compiled Objective-C app is using garbage collection?

前端 未结 2 1639
醉酒成梦
醉酒成梦 2021-02-20 12:55

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?

相关标签:
2条回答
  • 2021-02-20 13:28

    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?

    0 讨论(0)
  • 2021-02-20 13:37

    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.

    0 讨论(0)
提交回复
热议问题