查找类中字段的偏移量

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

Jol to find the offset of fields in the class

问题

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

import java.lang.reflect.Field;

public static void main(String[] args) throws NoSuchFieldException {
    System.out.println(ClassLayout.parseClass(Field.class).toPrintable());
}

The output:

OFF  SZ   TYPE DESCRIPTION               VALUE
  0   8        (object header: mark)     N/A
  8   4        (object header: class)    N/A
 12   4        (object alignment gap)    
Instance size: 16 bytes
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?
英文:

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

import java.lang.reflect.Field;

public static void main(String[] args) throws NoSuchFieldException {
    System.out.println(ClassLayout.parseClass(Field.class).toPrintable());
}

The output:

OFF  SZ   TYPE DESCRIPTION               VALUE
  0   8        (object header: mark)     N/A
  8   4        (object header: class)    N/A
 12   4        (object alignment gap)    
Instance size: 16 bytes
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字节。

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

输出摘录:

java.lang.reflect.Field对象内部:
OFF  SZ   TYPE DESCRIPTION               VALUE
  0   8        (对象头: 标记)             0x0000000000000005 (可偏移; 年龄: 0)
  8   4        (对象头: 类)              0x0006e918
 12   4        (对象对齐间隙)    
实例大小:72字节
空间损失: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.

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

Output excerpt:

java.lang.reflect.Field object internals:
OFF  SZ   TYPE DESCRIPTION               VALUE
  0   8        (object header: mark)     0x0000000000000005 (biasable; age: 0)
  8   4        (object header: class)    0x0006e918
 12   4        (object alignment gap)    
Instance size: 72 bytes
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:

确定