IntelliJ IDEA 2023.1 (Ultimate Edition)
Build #IU-231.8109.175, built on March 28, 2023
For educational use only.
Runtime version: 17.0.6+10-b829.5 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 2016M
Cores: 12
Registry:
debugger.new.tool.window.layout=true
debugger.valueTooltipAutoShowOnSelection=true
ide.experimental.ui=true
环境
Java 环境
java version "1.8.0_351" Java(TM) SE Runtime Environment (build 1.8.0_351-b10) Java HotSpot(TM) 64-Bit Server VM (build 25.351-b10, mixed mode)
JVM 参数默认值
通过命令
java -XX:+PrintFlagsFinal
查看参数的默认值可以发现 CompactFields 确实默认为 true,但是它似乎没有作用。IDE 环境
IntelliJ IDEA 2023.1 (Ultimate Edition) Build #IU-231.8109.175, built on March 28, 2023 For educational use only. Runtime version: 17.0.6+10-b829.5 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Windows 10.0 GC: G1 Young Generation, G1 Old Generation Memory: 2016M Cores: 12 Registry: debugger.new.tool.window.layout=true debugger.valueTooltipAutoShowOnSelection=true ide.experimental.ui=true
Non-Bundled Plugins: cn.com.pism.batslog (23.03.02.2009-RE) com.intellij.zh (231.250) leetcode-editor (8.7) com.intellij.ideolog (203.0.30.0) CMD Support (1.0.5) coderead.IdeaPlugins.maven (1.1) com.intellij.plugin.adernov.powershell (2.0.10) MavenRunHelper (4.23.222.2964.0) com.baomidou.plugin.idea.mybatisx (1.5.5) cn.yiiguxing.plugin.translate (3.4.2)
Kotlin: 231-1.8.20-IJ8109.175
Windows 环境
Win10 专业版 19045.2728
测试代码
从运行结果可以发现:父类变量在偏移量为 46 的位置出现了一个大小为 2 的间隙,但是 JVM 并没有将子类中大小为 1 的 boolean 变量插入到父类变量的间隙中。如果将子类的 boolean 变量插入到间隙中,这将使整个对象的大小缩减为 56 bytes,并且只浪费 1 bytes,而不是占用 64 bytes 浪费 9 bytes。这是为什么呢?我希望是我这对块知识的理解有误。