JVM如何打印GC时间?

时间:2016-02-02 06:08:36

标签: java garbage-collection jvm

我的java代码:

package com.v2ex;

public class Test {

    private static final int MB = 1024 * 1024;

    public static void main(String[] args) {
        byte[] bytes1, bytes2, bytes3, bytes4;
        bytes1 = new byte[2 * MB];
        bytes2 = new byte[2 * MB];
        bytes3 = new byte[2 * MB];
        bytes4 = new byte[4 * MB];
    }
}

java -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+ PrintGCDetails -XX:SurvivorRatio = 8 com / v2ex / Test:

Heap
 PSYoungGen      total 9216K, used 6799K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 83% used [0x00000000ff600000,0x00000000ffca3f28,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 4096K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 40% used [0x00000000fec00000,0x00000000ff000010,0x00000000ff600000)
 PSPermGen       total 21504K, used 2751K [0x00000000f4600000, 0x00000000f5b00000, 0x00000000fec00000)
  object space 21504K, 12% used [0x00000000f4600000,0x00000000f48afc08,0x00000000f5b00000)

我不知道GC花费多少时间。

1 个答案:

答案 0 :(得分:0)

您没有看到任何GC时间的原因是因为运行程序时没有发生GC。如果您为GC添加手动调用,如下面的代码所示,那么GC将获得更多输出。

private static final int MB = 1024 * 1024;

public static void main(String[] args) {
    byte[] bytes1, bytes2, bytes3, bytes4;
    bytes1 = new byte[2 * MB];
    bytes2 = new byte[2 * MB];
    bytes3 = new byte[2 * MB];
    bytes4 = new byte[4 * MB];
    System.gc();
}

突然间你也看到了这些线:

[GC(System.gc()) - [PSYoungGen:6979K-> 6979K(9216K)] 11075K-> 15179K(19456K),0.0063629秒] [时间:用户= 0.00 sys = 0.00,real = 0.01秒] [Full GC(System.gc())[PSYoungGen:6979K-> 2340K(9216K)] [ParOldGen:8200K-> 8193K(10240K)] 15179K-> 10533K(19456K),[Metaspace:2514K-> 2514K(1056768K)],0.0049945秒] [时间:用户= 0.00系统= 0.00,实际= 0.00秒]

编辑:没有GC发生的原因是因为你的程序运行得如此之快并且消耗的内存很少,所以没有GC需要。