在进行虚拟机测试的时候碰见一个不理解的现象代码如下:

package com.memory.gc;

import com.memory.tools.AddressPrint;

public class TenuringThreshold {

    private static final int _1MB = 1024 * 1024;

    @SuppressWarnings("unused")
    public static void testTenuringThreshold() {
        byte[] allocation1, allocation2, allocation3;

        try {
            allocation1 = new byte[_1MB / 4];
            System.out.println("allocation1:"+AddressPrint.addressOf(allocation1));
            allocation2 = new byte[4 * _1MB];
            System.out.println("allocation2:"+AddressPrint.addressOf(allocation2));
            allocation3 = new byte[5 * _1MB];
            System.out.println("allocation3:"+AddressPrint.addressOf(allocation3));
//            allocation3 = null;
//            System.out.println("allocation3:"+AddressPrint.addressOf(allocation3));
//            allocation3 = new byte[5 * _1MB];
//            System.out.println("allocation3:"+AddressPrint.addressOf(allocation3));

            System.out.println("allocation1:"+AddressPrint.addressOf(allocation1));
            System.out.println("allocation2:"+AddressPrint.addressOf(allocation2));
            System.out.println("allocation3:"+AddressPrint.addressOf(allocation3));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public static void main(String[] args) {
        TenuringThreshold.testTenuringThreshold();
    }


}

打印日志

"C:\Program Files\Java\jdk1.8.0_144\bin\java.exe" -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCCause "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=52772:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.6\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\rt.jar;D:\MyWorkSpace\JavaHotSpot\target\classes;D:\maven\Repository\cglib\cglib\3.2.7\cglib-3.2.7.jar;D:\maven\Repository\org\ow2\asm\asm\6.2\asm-6.2.jar;D:\maven\Repository\org\apache\ant\ant\1.10.3\ant-1.10.3.jar;D:\maven\Repository\org\apache\ant\ant-launcher\1.10.3\ant-launcher-1.10.3.jar" com.memory.gc.TenuringThreshold
allocation1:ff845f90
allocation2:ff885fa0
allocation3:fec00000
allocation1:ff845f90
allocation2:ff885fa0
allocation3:fec00000
Heap
 PSYoungGen      total 9216K, used 6844K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 83% used [0x00000000ff600000,0x00000000ffcaf080,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 5120K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 50% used [0x00000000fec00000,0x00000000ff100010,0x00000000ff600000)
 Metaspace       used 3494K, capacity 4500K, committed 4864K, reserved 1056768K
  class space    used 384K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

然后不是很理解,为什么没有发生GC,allocation3 为什么直接进入老年代了,然后查找资料,把直接进入老年代的情况列举一下:

  1. 分配的对象大小大于eden space。适合所有收集器。
  2. eden space剩余空间不足分配,且需要分配对象内存大小不小于eden space总空间的一半,直接分配到老年代,不触发Minor GC。适合-XX:+UseParallelGC、-XX:+UseParallelOldGC,即适合Parallel Scavenge。
  3. 大对象直接进入老年代,使用-XX:PretenureSizeThreshold参数控制,适合-XX:+UseSerialGC、-XX:+UseParNewGC、-XX:+UseConcMarkSweepGC,即适合Serial和ParNew收集器。
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐