查找类中字段的偏移量

huangapple go评论123阅读模式
英文:

Jol to find the offset of fields in the class

问题

I use jol to find the offset of the fields as below:

  1. import java.lang.reflect.Field;
  2. public static void main(String[] args) throws NoSuchFieldException {
  3. System.out.println(ClassLayout.parseClass(Field.class).toPrintable());
  4. }

The output:

  1. OFF SZ TYPE DESCRIPTION VALUE
  2. 0 8 (object header: mark) N/A
  3. 8 4 (object header: class) N/A
  4. 12 4 (object alignment gap)
  5. Instance size: 16 bytes
  6. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
  7. The 3rd row shows (object alignment gap) but didn't show the offset of the fields such as override, name, type, and such on.
  8. How can I get those field's offset? Or is there other ways to get the offset?
英文:

I use jol to find the offset of the fields as below:

  1. import java.lang.reflect.Field;
  2. public static void main(String[] args) throws NoSuchFieldException {
  3. System.out.println(ClassLayout.parseClass(Field.class).toPrintable());
  4. }

The output:

  1. OFF SZ TYPE DESCRIPTION VALUE
  2. 0 8 (object header: mark) N/A
  3. 8 4 (object header: class) N/A
  4. 12 4 (object alignment gap)
  5. Instance size: 16 bytes
  6. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

The 3rd row shows (object alignment gap) but didn't show the offset of the fields such as override, name, type and such on.

How can I get those field's offset? Or is there other ways to get the offset?

答案1

得分: 1

JOL IntelliJ Plugin显示偏移信息,显然。

安装插件,在您的代码中的某处使用Field,将光标放在文本Field上,然后转到Code -> Show Object Layout

请注意,还有一个选择器可以用于选择要使用的JVM实现。

有趣的是,使用JOL命令行工具的sudo报告了一个不同大小的Field对象 - 对于我的JVM,大小为72字节。

  1. $ sudo java -jar jol-cli-latest.jar internals java.lang.reflect.Field

输出摘录:

  1. java.lang.reflect.Field对象内部:
  2. OFF SZ TYPE DESCRIPTION VALUE
  3. 0 8 (对象头: 标记) 0x0000000000000005 (可偏移; 年龄: 0)
  4. 8 4 (对象头: ) 0x0006e918
  5. 12 4 (对象对齐间隙)
  6. 实例大小:72字节
  7. 空间损失:0字节内部 + 4字节外部 = 4字节总共
英文:

The JOL IntelliJ Plugin shows the offset information, apparently.

Install the plugin, use Field somewhere in your code, put the cursor on the text Field, then go to Code -> Show Object Layout

查找类中字段的偏移量

Note that there is also a selector for the JVM implementation to use.

Interestingly, using sudo with the JOL command line tool report a different size for a Field object - 72 bytes for my JVM.

  1. $ sudo java -jar jol-cli-latest.jar internals java.lang.reflect.Field

Output excerpt:

  1. java.lang.reflect.Field object internals:
  2. OFF SZ TYPE DESCRIPTION VALUE
  3. 0 8 (object header: mark) 0x0000000000000005 (biasable; age: 0)
  4. 8 4 (object header: class) 0x0006e918
  5. 12 4 (object alignment gap)
  6. Instance size: 72 bytes
  7. Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

huangapple
  • 本文由 发表于 2023年6月27日 19:02:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定