You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Object Layout是否支持Java Records?使用时触发不支持异常

Java Object Layout (JOL) 无法测量Java Records实例大小的问题

尝试用JOL测量Java Records实例的对象大小,调用GraphLayout.parseInstance或ClassLayout.parseClass时均抛出UnsupportedOperationException,错误提示为:

can't get field offset on a record class: private final int Outer$B.value

报错栈指向sun.misc.Unsafe.objectFieldOffset方法的第648行,根源是HotspotUnsafe.fieldOffset不支持Records类。

已尝试的无效操作

  • Java版本:从20降级至17
  • JOL版本:测试过0.17和0.16
  • 启动参数:添加了栈跟踪提示的-Djol.magicFieldOffset=true,问题依旧

复现代码

public class Outer {
    public static void main(String[] args) {
        // 正常运行 - 输出72
        System.out.println(GraphLayout.parseInstance(new A(1, "one")).totalSize());

        // 触发UnsupportedOperationException
        // "can't get field offset on a record class: private final int Outer$B.value"
        // sun.misc.Unsafe.objectFieldOffset - 第648行
        System.out.println(GraphLayout.parseInstance(new B(1, "one")).totalSize());
    }

    public static class A {
        int value;
        String name;

        public A(int value, String name) {
            this.value = value;
            this.name = name;
        }
    }

    public static record B(int value, String name) {
    }
}

疑问

搜索JOL与Java Records的使用示例但未找到,想确认:

  1. 当前JOL是否支持Java Records?
  2. 是否存在遗漏的配置项可以解决这个问题?

内容的提问来源于stack exchange,提问作者Donald Raab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 17:57:07