美文网首页
各种Java对象占用内存大小

各种Java对象占用内存大小

作者: designer | 来源:发表于2022-03-01 22:47 被阅读0次

    JOL的全称是Java Object Layout。是一个用来分析JVM中Object布局的小工具。包括Object在内存中的占用情况,实例对象的引用情况等等。

    引入依赖:

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

    相关API方法:

    private static void testObj() {
        Object obj = new Object();
        System.out.println(ClassLayout.parseInstance(obj).toPrintable());
    }
    

    1.object对象占用几个字节

    答案:16字节。
    其中对象头占用12字节,对齐填充4的字节。

    2.String对象占用几个字节

    答案JDK11版本中,占用24字节。
    其中对象头占用12字节,byte[]数组占用4字节,int类型的hash属性占用4字节,byte类型的coder占用1字节,对齐填充3个字节。

    java.lang.String object internals:
     OFFSET  SIZE     TYPE DESCRIPTION                               VALUE
          0     4          (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
          4     4          (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
          8     4          (object header)                           08 18 00 00 (00001000 00011000 00000000 00000000) (6152)
         12     4   byte[] String.value                              [97, 97, 97]
         16     4      int String.hash                               0
         20     1     byte String.coder                              0
         21     3          (loss due to the next object alignment)
    Instance size: 24 bytes
    Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
    

    3.Integer类型占用几个字节(基础类型的装箱)

    答案:占用16字节。
    其中对象头12字节,实例数据int占用4字节;

    java.lang.Integer object internals:
     OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
          0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
          4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
          8     4        (object header)                           e8 39 01 00 (11101000 00111001 00000001 00000000) (80360)
         12     4    int Integer.value                             100000
    Instance size: 16 bytes
    Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
    

    4.Long占用几个字节

    答案:占用24个字节。
    其中对象头12字节,实例数据long占用8字节,对齐填充4字节(必须是8的整数倍)。

    java.lang.Long object internals:
     OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
          0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
          4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
          8     4        (object header)                           38 3c 01 00 (00111000 00111100 00000001 00000000) (80952)
         12     4        (alignment/padding gap)                  
         16     8   long Long.value                                100000
    Instance size: 24 bytes
    Space losses: 4 bytes internal + 0 bytes external = 4 bytes total
    

    5.Object[100]占用几个字节

    答案:416字节。
    其中对象头16字节,对齐填充100*4=400字节。

    [Ljava.lang.Object; 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)                           88 3e 01 00 (10001000 00111110 00000001 00000000) (81544)
         12     4                    (object header)                           64 00 00 00 (01100100 00000000 00000000 00000000) (100)
         16   400   java.lang.Object Object;.<elements>                        N/A
    Instance size: 416 bytes
    Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
    

    5.String[100]占用几个字节

    答案:416字节。
    其中对象头16字节,String的实例数据+对齐填充占用400字节。

    [Ljava.lang.String; 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)                           58 55 02 00 (01011000 01010101 00000010 00000000) (152920)
         12     4                    (object header)                           64 00 00 00 (01100100 00000000 00000000 00000000) (100)
         16   400   java.lang.String String;.<elements>                        N/A
    Instance size: 416 bytes
    Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
    
    

    相关文章

      网友评论

          本文标题:各种Java对象占用内存大小

          本文链接:https://www.haomeiwen.com/subject/hlgjrrtx.html