您好,欢迎访问代理记账网站
移动应用 微信公众号 联系我们

咨询热线 -

电话 15988168888

联系客服
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

查看java对象内存占用

1、maven

<dependency>
	<groupId>org.openjdk.jol</groupId>
    <artifactId>jol-core</artifactId>
    <version>0.9</version>
</dependency>

2、ClassLayout.parseInstance 查看

@Data
public class MyTest {
    private int a;
    private boolean b;
}

//---
import org.openjdk.jol.info.ClassLayout;

public class LayoutDemo {
    public static void main(String[] args) {
        MyTest myTest = new MyTest();
        System.out.println("myTest:"+myTest);
        System.out.println(ClassLayout.parseInstance(myTest).toPrintable());
    }
}

结果

JVM 要求对象所占的内存空间大小是8字节的整数倍,而对象本来占17字节,加上补齐的 7 字节,一共占24字节(8字节 * 3)

myTest:MyTest(a=0, b=false)
# WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
com.pilaf.classlayout.MyTest object internals:
 OFFSET  SIZE      TYPE DESCRIPTION                               VALUE
      0     4           (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4           (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4           (object header)                           43 c1 00 f8 (01000011 11000001 00000000 11111000) (-134168253)
     12     4       int MyTest.a                                  0
     16     1   boolean MyTest.b                                  false
     17     7           (loss due to the next object alignment)
Instance size: 24 bytes
Space losses: 0 bytes internal + 7 bytes external = 7 bytes total

分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进