性能文章>Java GC详解 - 最全面的理解Java对象结构 - 对象指针 OOPs>

Java GC详解 - 最全面的理解Java对象结构 - 对象指针 OOPs原创

3年前
901325

最近在抽时间阅读 JDK 的源码,主要是 GC 还有 Safepoint 相关的源码,发现很多我在之前拜读网上各种 JVM 原理大作时候的由于没有看源码导致我对于底层原理的误解。果然,一百个人读水浒传,就有一百种水浒传。还是需要更加深入的了解下源码,才能更好地理解 JVM,进行调优。这个系列,将在讲述 Java GC 各种原理的基础上,结合对应的源码分析,并附上源码地址。因为JVM的源码更新还是很快的,尤其是 GC 这一块,但是基本原理,应该大体不会变,附上源码地址,旨在让各位读者掌握这些原理最新实现情况。本文在撰写的时候,会紧跟最新版本的 JDK。

GC(Garbage Collection) 是目前很多编程语言自带的特性,例如Java,Python;GC是一个很好的特性,能让使用这个语言编程的程序员不去关心内存回收,并且降低内存泄漏和内存溢出发生的概率。

了解Java GC,需要先知道 Java 最基础的对象在内存中究竟是如何存储的。我们专注于 HotSpot 虚拟机实现,来详细阐述对象存储结构。首先我们来了解一个概念,对象指针(OOPs,Ordinary Object Pointers), 也就是对象头的主要部分。

1. 对象指针(OOPs,Ordinary Object Pointers)

对象指针的实现,可以参考oop.hpp


class oopDesc {
 private:
  volatile markWord _mark;
  union _metadata {
    Klass*      _klass;
    narrowKlass _compressed_klass;
  } _metadata;
}
————————————————
版权声明:本文为CSDN博主「干货满满张哈希」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhxdick/article/details/108627011

从源代码可以简单看出,对象指针包括:

1.标记字(Mark Word): 一组标记,描述了对象的状态,包括对象默认哈希值(如果没有覆盖默认的 hashcode() 方法,则哈希值在 hashcode() 方法被调用之后,会被记录到标记字之中)、对象的形状(是否是数组)、锁状态(偏向锁等锁信息,值得一提的是偏向锁在 Java 15 中废弃:Disable and Deprecate Biased Locking)、数组长度(如果标记显示这个对象是数组,描述了数组的长度)。标记字的实现仅仅包含一个uintptr_t类型,所以,在 32 位和 64 位虚拟机上面,大小分别是 4 字节和 8 字节。可以参考源码:
markWord.hpp。我们这里只讨论 64 位的 JVM,也就是标记字为 8 字节的情况。


class markWord {
 private:
  uintptr_t _value;
}

2.类型字(Class Word): 类型字是指向对象实现的 Class 的指针。Java 7 之前指向的区域位于持久带(Permanent Generation),Java 8 之后,持久带废弃,引入了元数据区的概念(Metaspace),所以Java 8 之后指向的是这个元数据区。

这个指针可能是被压缩的,就是压缩指针(Compressed OOPs)。当开启对象压缩时占用4字节(JVM默认开启),关闭时占用8字节

1.1. 标记字的具体结构

对于 64 位的虚拟机环境,标记字大小是 8 字节。先给出标记字结构:

image.png
(上图来自于:https://www.cnblogs.com/helloworldcode/p/11914053.html)

我们先来通过 jol (Java Object Layout) 工具,以及接下来的几个实例,来逐步看一下每种状态下的标记字结构。加入依赖:


<!-- https://mvnrepository.com/artifact/org.openjdk.jol/jol-core -->
<dependency>
    <groupId>org.openjdk.jol</groupId>
    <artifactId>jol-core</artifactId>
    <version>0.13</version>
</dependency>


1.1.1. 哈希值计算几次?去哪里找哈希值?

如果类没有覆盖hashcode(),那么实现hashcode()的是一个native方法。


@HotSpotIntrinsicCandidate
public native int hashCode();


具体的实现参考源码synchronizer.cpp:


intptr_t ObjectSynchronizer::FastHashCode(Thread* self, oop obj) {
  //如果启用了偏向锁特性
  if (UseBiasedLocking) {
	//如果正处于偏向锁
    if (obj->mark().has_bias_pattern()) {
      //取消偏向锁,不会再处于偏向锁状态,并且在下次获取锁的时候,直接从轻量锁开始
      Handle hobj(self, obj);
      if (SafepointSynchronize::is_at_safepoint()) {
        BiasedLocking::revoke_at_safepoint(hobj);
      } else {
        BiasedLocking::revoke(hobj, self);
      }
      obj = hobj();
      assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
    }
  }

  while (true) {
    ObjectMonitor* monitor = NULL;
    markWord temp, test;
    intptr_t hash;
    markWord mark = read_stable_mark(obj);

    // 不能处于偏向锁状态
    assert(!mark.has_bias_pattern(), "invariant");
    // 未处于任何锁状态的一般状态
    if (mark.is_neutral()) {            
      hash = mark.hash();
      if (hash != 0) {                  // 如果已经计算过哈希值,直接返回
        return hash;
      }
      hash = get_next_hash(self, obj);  // 否则,计算一个新的哈希值,这个方法的详细分析在另一篇文章
      temp = mark.copy_set_hash(hash);  // 设置哈希值
      test = obj->cas_set_mark(temp, mark); //更新为设置成功
      if (test == mark) {               // 如果更新成功,则返回
        return hash;
      }
      //如果设置失败,可能发生了锁膨胀,或者是多线程同时调用了hashcode方法,循环重新尝试
	
    } else if (mark.has_monitor()) {
	  //如果处于重量级锁状态
	  //获取对应的monitor对象
      monitor = mark.monitor();
	  //获取其header
      temp = monitor->header();
      assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
	  //查看header中的hashcode
      hash = temp.hash();
	  //若monitor中有哈希值,并且没有发生异步monitor降级(Async Monitor Deflation),就使用这个哈希值
	  //异步monitor降级(Async Monitor Deflation),请参考:https://wiki.openjdk.java.net/display/HotSpot/Async+Monitor+Deflation,这是在 Java 15 之后开始引入的新特性
      if (hash != 0) {
        // It has a hash.

        // Separate load of dmw/header above from the loads in
        // is_being_async_deflated().
        if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
          // A non-multiple copy atomic (nMCA) machine needs a bigger
          // hammer to separate the load above and the loads below.
          OrderAccess::fence();
        } else {
          OrderAccess::loadload();
        }
        if (monitor->is_being_async_deflated()) {
          // But we can't safely use the hash if we detect that async
          // deflation has occurred. So we attempt to restore the
          // header/dmw to the object's header so that we only retry
          // once if the deflater thread happens to be slow.
          monitor->install_displaced_markword_in_object(obj);
          continue;
        }
        return hash;
      }
	
    } else if (self->is_lock_owned((address)mark.locker())) {
      //如果处于轻量锁状态,读取指向锁记录的指针,获取其中的哈希值
      temp = mark.displaced_mark_helper();
      assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
      hash = temp.hash();
      if (hash != 0) {                  // if it has a hash, just return it
        return hash;
      }
    }
	//不处于以上状态或者处于以上锁状态之前,没有计算过哈希值,则直接膨胀锁到重量级锁
    monitor = inflate(self, obj, inflate_cause_hash_code);
	//查看是否已经有其他线程计算过哈希值
    mark = monitor->header();
    assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
    hash = mark.hash();
	//如果没有,则在这里计算,并设置
    if (hash == 0) {                    // if it does not have a hash
      hash = get_next_hash(self, obj);  // get a new hash
      temp = mark.copy_set_hash(hash);  // merge the hash into header
      assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
      uintptr_t v = Atomic::cmpxchg((volatile uintptr_t*)monitor->header_addr(), mark.value(), temp.value());
      test = markWord(v);
	  //设置失败,则有其他线程已经设置,尝试读取
      if (test != mark) {
        // The attempt to update the ObjectMonitor's header/dmw field
        // did not work. This can happen if another thread managed to
        // merge in the hash just before our cmpxchg().
        // If we add any new usages of the header/dmw field, this code
        // will need to be updated.
        hash = test.hash();
        assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value());
        assert(hash != 0, "should only have lost the race to a thread that set a non-zero hash");
      }
	  //如果有发生异步monitor降级(Async Monitor Deflation),跳过,重新判断
	  //异步monitor降级(Async Monitor Deflation),请参考:https://wiki.openjdk.java.net/display/HotSpot/Async+Monitor+Deflation,这是在 Java 15 之后开始引入的新特性
      if (monitor->is_being_async_deflated()) {
        // If we detect that async deflation has occurred, then we
        // attempt to restore the header/dmw to the object's header
        // so that we only retry once if the deflater thread happens
        // to be slow.
        monitor->install_displaced_markword_in_object(obj);
        continue;
      }
    }
    // We finally get the hash.
    return hash;
  }
}



可以看出,hashcode:

  1. 尽量只计算一次,计算出后,对于无锁对象,保存在对象标记字 Markword 中。
  2. 处于各种锁状态的话(除了偏向锁),都会修改并占用 Markword 导致需要其他地方缓存计算好的 hashcode,对于重量锁是对应的 Monitor 中保存,对于轻量锁是所指向的锁记录的指针中保存。
  3. 对于偏向锁,由于没法哈希值,所以只要计算过哈希值,就不会再进入偏向锁的状态,而是直接从轻量锁开始。
  4. 对于 JDK 15 之后引入的异步 Monitor 降级(Async Monitor Deflation),需要在这个过程完成或者未开始的时候读取 monitor对象的 hashcode 缓存。对于这个特性的详细说明,可以参考:Async Monitor Deflation

哈希值的具体计算,根据全局变量hashcode的值决定计算方式(通过-XX:hashCode=设定)。默认的哈希值计算,是hashcode=5 的情况,不同情况的计算方式的详细说明。

我们来通过一段代码以及输出来看看这些特性


public static void main(String[] args) throws Exception {
    A a = new A();
    B b = new B();
    System.out.println("------After Initialization------\n" + ClassLayout.parseInstance(a).toPrintable() + "\n" + ClassLayout.parseInstance(b).toPrintable());

    System.out.println("a.hashcode: " + a.hashCode());
    System.out.println("b.hashcode: " + b.hashCode());
    System.out.println("------After call hashcode------\n" + ClassLayout.parseInstance(a).toPrintable() + "\n" + ClassLayout.parseInstance(b).toPrintable());
}

//A没有覆盖默认的 hashcode 方法
public static class A {
    long d;
}
//B覆盖了 hashcode 方法
public static class B {
    long d;

    @Override
    public int hashCode() {
        return (int) 5555;
    }
}


输出:


------After Initialization------
com.hashjang.jdk.TestObjectAlign$A 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)                           49 ce 00 20 (01001001 11001110 00000000 00100000) (536923721)
     12     4        (alignment/padding gap)                  
     16     8   long A.d                                       0
Instance size: 24 bytes
Space losses: 4 bytes internal + 0 bytes external = 4 bytes total

com.hashjang.jdk.TestObjectAlign$B 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)                           8a ce 00 20 (10001010 11001110 00000000 00100000) (536923786)
     12     4        (alignment/padding gap)                  
     16     8   long B.d                                       0
Instance size: 24 bytes
Space losses: 4 bytes internal + 0 bytes external = 4 bytes total

a.hashcode: 2124046270
b.hashcode: 5555
------After call hashcode------
com.hashjang.jdk.TestObjectAlign$A object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           01 be 5f 9a (00000001 10111110 01011111 10011010) (-1705001471)
      4     4        (object header)                           7e 00 00 00 (01111110 00000000 00000000 00000000) (126)
      8     4        (object header)                           49 ce 00 20 (01001001 11001110 00000000 00100000) (536923721)
     12     4        (alignment/padding gap)                  
     16     8   long A.d                                       0
Instance size: 24 bytes
Space losses: 4 bytes internal + 0 bytes external = 4 bytes total

com.hashjang.jdk.TestObjectAlign$B 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)                           8a ce 00 20 (10001010 11001110 00000000 00100000) (536923786)
     12     4        (alignment/padding gap)                  
     16     8   long B.d                                       0
Instance size: 24 bytes
Space losses: 4 bytes internal + 0 bytes external = 4 bytes total




可以看出,对于未覆盖hashcode()方法的 A 类,调用hashcode()之后,哈希值存在了对象头。哈希值为 2124046270,转换为二进制为:1111110 10011010 01011111 10111110。由于 Windows 系统中为小端存储,所以打印出来的 Header 是反过来的,可以看到这个哈希值就存储在 Header 中:

00000001 10111110 01011111 10011010 01111110 00000000 00000000 00000000

其他特性,将在接下来的 1.1.3. 偏向锁,轻量锁,重量锁 这一小节详细说明。

1.1.2. 分代年龄

对象头中的分代年龄,用于分代 GC。分代 GC 我们在后面的章节会详细讲述,这里只是看一些特性。

记录分代年龄一共 4 bit,所以最大为 2^4 - 1 = 15。所以配置最大分代年龄-XX:MaxTenuringThreshold=n这个n不能大于15,当然也不能小于 0.等于 0 的话,就直接入老年代。等于 16 的话,就是从不进入老年代,这样不符合JVM规范,所以不能大于15。默认是 15。

在发生 Young GC,更准确地说是在 Survivor 区复制的时候,存活的对象的分代年龄会加1。我们编写程序测试下,由于 编译器会优化代码,同时调用System.gc()并不是立刻触发 GC,并且是 Full GC,可能会使对象直接进入老年代,分代年龄不再增长,所以我们可以使用 volatile 属性辅助我们真正创建对象,避免编译器优化:


static volatile Object consumer;
public static void main(String[] args) throws Exception {
    //这是我们要观察的对象
    Object instance = new Object();
    long lastAddr = VM.current().addressOf(instance);
    for (int i = 0; i < 10000; i++) {
        //查看地址是否发生了变化,代表是否发生了 Survivor 复制,或者是移动到老年代
        long currentAddr = VM.current().addressOf(instance);
        if (currentAddr != lastAddr) {
            //地址发生变化的时候,打印对象结构
            ClassLayout layout = ClassLayout.parseInstance(instance);
            System.out.println(layout.toPrintable());
            lastAddr = currentAddr;
        }
        for (int j = 0; j < 10000; j++) {
            //一直创建新对象
            //因为是volatile的属性更新,不会被编译器优化
            consumer = new Object();
        }
    }
}


可以配合 GC 日志一起观察。

首先我们用这个参数运行程序-Xmx128m -Xlog:gc=info,输出:


[0.016s][info][gc] Using G1
# WARNING: Unable to get Instrumentation. Dynamic Attach failed. You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf
[2.540s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 24M->1M(128M) 2.600ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           0d 00 00 00 (00001101 00000000 00000000 00000000) (13)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[2.627s][info][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.273ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           15 00 00 00 (00010101 00000000 00000000 00000000) (21)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[2.675s][info][gc] GC(2) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.063ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           1d 00 00 00 (00011101 00000000 00000000 00000000) (29)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[2.724s][info][gc] GC(3) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.068ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           25 00 00 00 (00100101 00000000 00000000 00000000) (37)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[2.772s][info][gc] GC(4) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.212ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           2d 00 00 00 (00101101 00000000 00000000 00000000) (45)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[2.821s][info][gc] GC(5) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.202ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           35 00 00 00 (00110101 00000000 00000000 00000000) (53)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[2.869s][info][gc] GC(6) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.143ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           3d 00 00 00 (00111101 00000000 00000000 00000000) (61)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[2.917s][info][gc] GC(7) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.313ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           45 00 00 00 (01000101 00000000 00000000 00000000) (69)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[2.969s][info][gc] GC(8) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.473ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           4d 00 00 00 (01001101 00000000 00000000 00000000) (77)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[3.021s][info][gc] GC(9) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.283ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           55 00 00 00 (01010101 00000000 00000000 00000000) (85)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[3.072s][info][gc] GC(10) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.648ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           5d 00 00 00 (01011101 00000000 00000000 00000000) (93)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[3.122s][info][gc] GC(11) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.585ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           65 00 00 00 (01100101 00000000 00000000 00000000) (101)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[3.173s][info][gc] GC(12) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.130ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           6d 00 00 00 (01101101 00000000 00000000 00000000) (109)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[3.224s][info][gc] GC(13) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.078ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           75 00 00 00 (01110101 00000000 00000000 00000000) (117)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[3.273s][info][gc] GC(14) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.135ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           7d 00 00 00 (01111101 00000000 00000000 00000000) (125)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[3.322s][info][gc] GC(15) Pause Young (Normal) (G1 Evacuation Pause) 75M->1M(128M) 2.467ms
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           7d 00 00 00 (01111101 00000000 00000000 00000000) (125)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           00 02 00 20 (00000000 00000010 00000000 00100000) (536871424)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

[3.404s][info][gc] GC(16) Pause Young (Normal) (G1 Evacuation Pause) 76M->1M(128M) 0.556ms
[3.485s][info][gc] GC(17) Pause Young (Normal) (G1 Evacuation Pause) 76M->1M(128M) 0.303ms
[3.566s][info][gc] GC(18) Pause Young (Normal) (G1 Evacuation Pause) 76M->1M(128M) 0.288ms
[3.647s][info][gc] GC(19) Pause Young (Normal) (G1 Evacuation Pause) 76M->1M(128M) 0.317ms
[3.727s][info][gc] GC(20) Pause Young (Normal) (G1 Evacuation Pause) 76M->1M(128M) 0.286ms


可以看到,在第 15 次 GC 的时候,对象进入了老年代,内存地址不再随着 Young GC 的进行而变化。

各位读者可以将-XX:MaxTenuringThreshold=设置为 0 和 16 看看效果。

1.1.3. 偏向锁,轻量锁,重量锁

我们来编写测试代码:


A a = new A();
System.out.println("------After Initialization------\n" + ClassLayout.parseInstance(a).toPrintable());
//偏向锁
synchronized (a) {
    System.out.println("------After Fetched Lock------\n" + ClassLayout.parseInstance(a).toPrintable());
}
System.out.println("------After Released Lock------\n" + ClassLayout.parseInstance(a).toPrintable());

System.out.println("a.hashcode: " + a.hashCode());
System.out.println("------After call hashcode------\n" + ClassLayout.parseInstance(a).toPrintable());
//由于调用了 hashcode,这里直接升级成为轻量锁
synchronized (a) {
    System.out.println("------After Fetched Lock------\n" + ClassLayout.parseInstance(a).toPrintable());
}
System.out.println("------After Released Lock------\n" + ClassLayout.parseInstance(a).toPrintable());
//测试重量级锁
Runnable r = () -> {
    synchronized (a) {
        System.out.println("------After " + Thread.currentThread() + " lock is fetched------\n" + ClassLayout.parseInstance(a).toPrintable());
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};
Thread [] threads = new Thread[2];
for (int i = 0; i < threads.length; i++) {
    threads[i] = new Thread(r);
    threads[i].start();
}
for (int i = 0; i < threads.length; i++) {
    threads[i].join();
}

System.out.println("------After Released Lock------\n" + ClassLayout.parseInstance(a).toPrintable());



输出为(我们这里省略掉我们不关心的输出):


------After Initialization------
 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)

------After Fetched Lock------
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           05 90 8a 5e (00000101 10010000 10001010 01011110) (1586139141)
      4     4        (object header)                           c7 02 00 00 (11000111 00000010 00000000 00000000) (711)
 
------After Released Lock------
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           05 90 8a 5e (00000101 10010000 10001010 01011110) (1586139141)
      4     4        (object header)                           c7 02 00 00 (11000111 00000010 00000000 00000000) (711)

a.hashcode: 929776179
------After call hashcode------
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           01 33 42 6b (00000001 00110011 01000010 01101011) (1799500545)
      4     4        (object header)                           37 00 00 00 (00110111 00000000 00000000 00000000) (55)

------After Fetched Lock------
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           78 f2 0f 53 (01111000 11110010 00001111 01010011) (1393554040)
      4     4        (object header)                           ee 00 00 00 (11101110 00000000 00000000 00000000) (238)

------After Released Lock------
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           01 33 42 6b (00000001 00110011 01000010 01101011) (1799500545)
      4     4        (object header)                           37 00 00 00 (00110111 00000000 00000000 00000000) (55)
	  
------After Thread[Thread-0,5,main] lock is fetched------
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           02 3e e0 b0 (00000010 00111110 11100000 10110000) (-1327481342)
      4     4        (object header)                           69 02 00 00 (01101001 00000010 00000000 00000000) (617)
      
------After Thread[Thread-1,5,main] lock is fetched------
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           02 3e e0 b0 (00000010 00111110 11100000 10110000) (-1327481342)
      4     4        (object header)                           69 02 00 00 (01101001 00000010 00000000 00000000) (617)
      
------After Released Lock------
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           02 3e e0 b0 (00000010 00111110 11100000 10110000) (-1327481342)
      4     4        (object header)                           69 02 00 00 (01101001 00000010 00000000 00000000) (617)


我们这里通过第一字节 8 位的末尾两位判断锁状态:

  • 创建对象后,初始为无锁状态:第一字节为00000101,01 代表处于无锁状态。同时,偏向锁是开启状态,因为00000101,倒数第三位为1,这个根据前面的结构图可以知道是偏向锁标记。
  • 第一次 main 线程获取锁,由于没有争抢,同时启动参数中也没有关闭偏向锁,采用偏向锁:第一字节为00000101,代表处于偏向锁状态,后面保存的是指向线程的指针。
  • 第一次 main 线程释放锁,由于没有其他争抢,保持这个偏向锁状态(Hotspot 取消偏向锁需要全局 safepoint。
  • 调用 hashcode,根据之前的源码分析,需要取消偏向锁,同时将 hashcode 写入 header:第一字节为00000001,代表处于无锁状态,偏向锁关闭,倒数第三位为 0。
  • 调用 hashcode 之后 main 线程获取锁,由于偏向锁关闭,直接从轻量锁开始:第一字节为01111000,00 代表轻量锁。后面保存了指向锁记录的指针。
  • 调用 hashcode 之后 main 线程释放锁,释放轻量锁,锁记录会被回收,所以 hashcode 回到 header 保存。
  • 多线程导致对象升级为重量级锁之后:第一字节为 00000010,10 代表重量级锁,由于 monitor 一旦生成一直存在,所以这个对象头会一直保留 monitor 的指针,hashcode 也会保存在 monitor 上。
  • 最后锁释放后,header 没有改变,也是上面说的原因。

1.2. 类型字压缩指针与 JVM 最大内存

压缩指针这个属性默认是打开的,可以通过-XX:-UseCompressedOops关闭。

首先说一下为何需要压缩指针呢?32 位的存储,可以描述多大的内存呢?假设每一个1代表1字节,那么可以描述 0~2^32-1 这 2^32 字节也就是 4 GB 的内存。

image.png

但是呢,Java 默认是 8 字节对齐的内存,也就是一个对象占用的空间,必须是 8 字节的整数倍,不足的话会填充到 8 字节的整数倍。也就是其实描述内存的时候,不用从 0 开始描述到 8(就是根本不需要定位到之间的1,2,3,4,5,6,7)因为对象起止肯定都是 8 的整数倍。所以,2^32 字节如果一个1代表8字节的话,那么最多可以描述 2^32 * 8 字节也就是 32 GB 的内存。

image.png

这就是压缩指针的原理。如果配置最大堆内存超过 32 GB(当 JVM 是 8 字节对齐),那么压缩指针会失效。 但是,这个 32 GB 是和字节对齐大小相关的,也就是-XX:ObjectAlignmentInBytes配置的大小(默认为8字节,也就是 Java 默认是 8 字节对齐)。-XX:ObjectAlignmentInBytes可以设置为 8 的整数倍,最大 128。也就是如果配置-XX:ObjectAlignmentInBytes为 24,那么配置最大堆内存超过 96 GB 压缩指针才会失效。

编写程序测试下:


A a = new A();
System.out.println("------After Initialization------\n" + ClassLayout.parseInstance(a).toPrintable());


首先,以启动参数:-XX:ObjectAlignmentInBytes=8 -Xmx16g执行:


------After Initialization------
com.hashjang.jdk.TestObjectAlign$A 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)                           48 72 06 00 (01001000 01110010 00000110 00000000) (422472)
     12     4        (alignment/padding gap)                  
     16     8   long A.d                                       0
Instance size: 24 bytes
Space losses: 4 bytes internal + 0 bytes external = 4 bytes total


可以看到类型字大小为 4 字节48 72 06 00 (01001000 01110010 00000110 00000000) (422472),压缩指针生效。

首先,以启动参数:-XX:ObjectAlignmentInBytes=8 -Xmx32g执行:


------After Initialization------
com.hashjang.jdk.TestObjectAlign$A 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)                           a0 5b c6 00 (10100000 01011011 11000110 00000000) (12999584)
     12     4        (object header)                           b4 02 00 00 (10110100 00000010 00000000 00000000) (692)
     16     8   long A.d                                       0
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total


可以看到类型字大小为 8 字节,压缩指针失效:


a0 5b c6 00 (10100000 01011011 11000110 00000000) (12999584)
b4 02 00 00 (10110100 00000010 00000000 00000000) (692)


修改对齐大小为 16 字节,也就是以-XX:ObjectAlignmentInBytes=16 -Xmx32g执行:


------After Initialization------
com.hashjang.jdk.TestObjectAlign$A 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)                           48 72 06 00 (01001000 01110010 00000110 00000000) (422472)
     12     4        (alignment/padding gap)                  
     16     8   long A.d                                       0
     24     8        (loss due to the next object alignment)
Instance size: 32 bytes
Space losses: 4 bytes internal + 8 bytes external = 12 bytes total


可以看到类型字大小为 4 字节48 72 06 00 (01001000 01110010 00000110 00000000) (422472),压缩指针生效。

总结

  1. 对象指针包括标记字与类型字。
  2. 标记字中保存了:对象默认哈希值(如果没有覆盖默认的 hashcode() 方法,则哈希值在 hashcode() 方法被调用之后,会被记录到标记字之中)、对象的形状(是否是数组)、锁状态(偏向锁等锁信息,值得一提的是偏向锁在 Java 15 中废弃:Disable and Deprecate Biased Locking)、数组长度(如果标记显示这个对象是数组,描述了数组的长度)。标记字的实现仅仅包含一个uintptr_t类型,所以,在 32 位和 64 位虚拟机上面,大小分别是 4 字节和 8 字节。可以参考源码:
    markWord.hpp
  3. 类型字中保存了:指向对象实现的 Class 的指针。类型字默认是被压缩的,压缩指针指的就是这里。
  4. 默认哈希值计算,对于偏向锁是否生效,是有影响的。就是默认哈希值与偏向锁不能共存。
  5. 默认哈希值有缓存:无锁缓存在标记字;轻量锁缓存在锁记录,标记字中有指针指向锁记录,轻量锁释放后,锁记录中的哈希值复制到标记字中;重量锁缓存在 monitor 对象,标记字中有指针指向 monitor 对象,释放后,哈希值依然缓存在 monitor 对象中。
  6. 默认哈希值计算,需要考虑异步 monitor 降级的情况,这是 Java 15 中的新特性:Async Monitor Deflation
  7. 分代年龄在每次 Young GC 复制之后 +1,最大是 -XX:MaxTenuringThreshold=n配置的值,大于这个值就进入老年代了。
  8. 压缩指针是否启用和 Java 对齐字节大小(-XX:ObjectAlignmentInBytes,默认是 8,也就是 8 字节对齐)还有最大堆栈大小相关。
点赞收藏
张哈希
请先登录,查看2条精彩评论吧
快去登录吧,你将获得
  • 浏览更多精彩评论
  • 和开发者讨论交流,共同进步

为你推荐

随机一门技术分享之Netty

随机一门技术分享之Netty

MappedByteBuffer VS FileChannel:从内核层面对比两者的性能差异

MappedByteBuffer VS FileChannel:从内核层面对比两者的性能差异

5
2