如何确定已编译的Objective-C应用程序是否正在使用垃圾回收?

时间:2022-09-07 08:18:03

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?

对于我在Mac上的任何应用程序,有没有办法判断它是在启用GC的情况下编译的,还是在进行手动内存管理?

2 个解决方案

#1


19  

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.

我在这里找到了答案。请注意,原始帖子是错误的,但包含Apple工程师Mark Rowe的评论。

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

我用当前的OS(10.6.4)重新运行他在我的机器上提到的otool命令。这是输出:

$ 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:

Mark Rowe的解释:

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.

这里感兴趣的领域是__OBJC段的__image_info部分的“flags”字段。如果支持垃圾收集,它将具有值0×2并将显示为“GC RR”以表示支持垃圾收集和保留/释放。如果需要垃圾收集,那么该字段的值将为0×4,并将显示为“仅GC”,表示仅支持垃圾收集且保留/释放不可用。该字段还可以包含其他值,但这两个值是与垃圾收集相关的唯一值。

#2


5  

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

在mach-o中是一个标志,用于确定是否使用GC支持编译的二进制文件,非GC或混合模式。

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

我不知道通过更友好的API查询这些位的任何事情。

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

Objective-C运行时中的markgc.c源可以读取所述标志。您可以根据需要重构它以满足您的需求。

Kind of curious why you need to know?

有点好奇为什么你需要知道?

#1


19  

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.

我在这里找到了答案。请注意,原始帖子是错误的,但包含Apple工程师Mark Rowe的评论。

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

我用当前的OS(10.6.4)重新运行他在我的机器上提到的otool命令。这是输出:

$ 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:

Mark Rowe的解释:

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.

这里感兴趣的领域是__OBJC段的__image_info部分的“flags”字段。如果支持垃圾收集,它将具有值0×2并将显示为“GC RR”以表示支持垃圾收集和保留/释放。如果需要垃圾收集,那么该字段的值将为0×4,并将显示为“仅GC”,表示仅支持垃圾收集且保留/释放不可用。该字段还可以包含其他值,但这两个值是与垃圾收集相关的唯一值。

#2


5  

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

在mach-o中是一个标志,用于确定是否使用GC支持编译的二进制文件,非GC或混合模式。

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

我不知道通过更友好的API查询这些位的任何事情。

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

Objective-C运行时中的markgc.c源可以读取所述标志。您可以根据需要重构它以满足您的需求。

Kind of curious why you need to know?

有点好奇为什么你需要知道?