JVM内存分配策略

JDK版本1.8

1.对象优先在Eden分配

package com.company;

/**
 * @author ThomasZou
 * @date 2018/10/17 14:31
 * @description
 */
public class TestAllocation {

    private static final int _1MB = 1024 * 1024;

    /**
     * 参数 -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:+UseSerialGC
     * @param args
     */
    public static void main(String[] args) {
        byte[] allocation1, allocation2, allocation3, allocation4;
        allocation1 = new byte[2 * _1MB];
        allocation2 = new byte[2 * _1MB];
        allocation3 = new byte[2 * _1MB];
        allocation4 = new byte[4 * _1MB];
    }
}

控制台打印信息如下:

[GC (Allocation Failure) [DefNew: 6144K->708K(9216K), 0.0093255 secs] 6144K->4804K(19456K), 0.0094376 secs] [Times: user=0.00 sys=0.02, real=0.01 secs] 
Heap
 def new generation   total 9216K, used 7176K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  78% used [0x00000000fec00000, 0x00000000ff250eb8, 0x00000000ff400000)
  from space 1024K,  69% used [0x00000000ff500000, 0x00000000ff5b1398, 0x00000000ff600000)
  to   space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
 tenured generation   total 10240K, used 4096K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  40% used [0x00000000ff600000, 0x00000000ffa00020, 0x00000000ffa00200, 0x0000000100000000)
 Metaspace       used 3341K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 364K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

根据控制台打印的信息,我们发现给allocation4分配内存的时候,Eden已经占用了6MB,剩余的空间已经不足以存下allocation4,因此发生MinorGC。GC期间虚拟机又发现3个2MB大小的对象无法存入Survivor空间,因此通过分配担保机制提前转移到老年代。

2.大对象直接进入老年代

虚拟机提供-XX:PretenureSizeThreshold参数,令大于这个设置值的对象直接在老年代分配。目的是避免Eden区及两个Survivor区之间发生大量的内存复制。
注意:这个参数只对SerialParNew两款收集器有效,Parallel Scanvenge不认识这个参数所以一般不需要配置,如果遇到必须使用此参数的场合,可以考虑ParNewCMS收集器的组合

/**
 * @author ThomasZou
 * @date 2018/10/17 15:23
 * @description
 */
public class T2 {

    private static final int _1MB = 1024 * 1024;

    /**
     * JVM 参数:-Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails 
     *          -XX:+UseSerialGC -XX:SurvivorRatio=8 -XX:PretenureSizeThreshold=3145728
     */
    public static void main(String[] args) {
        byte [] allocation;
        allocation = new byte[ 4 * _1MB];
    }
}

控制台打印:

Heap
 def new generation   total 9216K, used 2212K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  27% used [0x00000000fec00000, 0x00000000fee293f8, 0x00000000ff400000)
  from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
  to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 tenured generation   total 10240K, used 4096K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  40% used [0x00000000ff600000, 0x00000000ffa00010, 0x00000000ffa00200, 0x0000000100000000)
 Metaspace       used 3169K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 345K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

根据控制台打印的信息,Eden空间使用了27%,老年代使用了40%,也就是4MB的allocation对象直接进入了老年代。

3.长期存活对象将进入老年代

如果对象在Eden出生之后经过第一次MinorGC仍然存在,并且能被Survivor容纳的话,将被移动到Survivor中,并设定对象年龄为1。此后,对象在Survivor中每经过一次Minor GC,年龄就增加1岁,当它的年龄增加到15岁时,就会被晋升到老年代中。这个晋升的阈值可以通过参数-XX:MaxTenuringThreshold设置

package com.company;

import org.junit.Test;

/**
 * @author ThomasZou
 * @date 2018/10/17 15:23
 * @description
 */
public class T2 {

    private static final int _1MB = 1024 * 1024;

    /**
     * JVM 参数:-Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails 
     * -XX:+UseSerialGC -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=1 
     * -XX:+PrintTenuringDistribution
     */
    public static void main(String[] args) {

        byte[] allocation1, allocation2, allocation3, allocation4;

        allocation1 = new byte[_1MB / 4];
        allocation2 = new byte[4 * _1MB];
        allocation3 = null;
        allocation3 = new byte[4 * _1MB];
        allocation4 = new byte[4 * _1MB];
    }
}

当设置`MaxTenuringThreshold=1 时控制台打印的数据

[GC (Allocation Failure) [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
- age   1:     989928 bytes,     989928 total
: 6568K->966K(9216K), 0.0094036 secs] 6568K->5062K(19456K), 0.0095198 secs] [Times: user=0.00 sys=0.02, real=0.01 secs] 
[GC (Allocation Failure) [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
- age   1:        256 bytes,        256 total
: 5146K->0K(9216K), 0.0051560 secs] 9242K->5056K(19456K), 0.0052130 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 def new generation   total 9216K, used 4315K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  52% used [0x00000000fec00000, 0x00000000ff036b90, 0x00000000ff400000)
  from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400100, 0x00000000ff500000)
  to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 tenured generation   total 10240K, used 5056K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  49% used [0x00000000ff600000, 0x00000000ffaf02b8, 0x00000000ffaf0400, 0x0000000100000000)
 Metaspace       used 3361K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 369K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

当设置-XX:MaxTenuringThreshold=1时,allocation1对象在第二次GC发生时进入老年代,新生代的内存GC之后变成0KB。
当设置MaxTenuringThreshold=15时控制台打印的数据

[GC (Allocation Failure) [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 15)
- age   1:     988000 bytes,     988000 total
: 6400K->964K(9216K), 0.0091353 secs] 6400K->5060K(19456K), 0.0092566 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
- age   1:        240 bytes,        240 total
: 5142K->0K(9216K), 0.0038030 secs] 9238K->5055K(19456K), 0.0038570 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 def new generation   total 9216K, used 4315K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  52% used [0x00000000fec00000, 0x00000000ff036e60, 0x00000000ff400000)
  from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff4000f0, 0x00000000ff500000)
  to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 tenured generation   total 10240K, used 5054K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  49% used [0x00000000ff600000, 0x00000000ffaefb50, 0x00000000ffaefc00, 0x0000000100000000)
 Metaspace       used 3343K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 364K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

而当第二次GC发生之后,allocation1的对象理论上应该残留在新生代,但是这里的情况和-XX:MaxTenuringThreshold=1时相同,因此可能是JDK版本的问题。

4.动态对象年龄判定

为了适应不同程序的内存状况,虚拟机并不是永远的要求对象的年龄必须达到MaxTenuringThreshold才能晋升老年代,如果在Survivor空间中相同年龄所有对象大小的总和大于Survivor空间的一半,年龄大于或等于该年龄对象就可以直接进入老年代,无需等到MaxTenuringThreshold中要求的年龄。

5.空间分配担保

在发生Minor GC之前,虚拟机会先检查老年大最大可用的连续空间是否大于新生代所有对象总空间,如果这个条件成立,那么Minor GC可以确保是安全的。如果不成立,虚拟机则会查看HandlePromotionFailure设置值是否允许担保失败。如果允许,则会继续检查老年代最大可用连续空间是否大于历次晋升到老年代对象的平均大小,如果大于将尝试着进行一次Minor GC,尽管这次GC是有风险的。如果小于,或者HandlePromotionFailure设置不允许冒险,则会进行Full GC

你可能感兴趣的:(JVM内存分配策略)