英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论