Java JVM如何打印GC时间?

Java JVM如何打印GC时间?,java,garbage-collection,jvm,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

我的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:SurviorRatio=8 com/v2ex/测试:

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花费了多少时间。

之所以看不到任何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秒][次:user=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秒][次:user=0.00 sys=0.00,real=0.00秒]


编辑:没有GC发生的原因是您的程序完成得太快,占用的内存太少,因此不需要GC。

谢谢,bytes4直接移动到ParOldGen。