简体   繁体   English

JVM如何打印GC时间?

[英]How does JVM print GC time?

My java code: 我的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: 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)

I don't how much time GC costs. 我不知道GC花费多少时间。

The reason you do not see any GC time is because there is no GC happening when running your program. 您没有看到任何GC时间的原因是因为运行程序时没有发生GC。 If you add a manual call for a GC like in the code below you will get more output with the time the GC took. 如果像下面的代码中那样手动调用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();
}

Suddenly you also see these lines: 突然,您还会看到以下几行:

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

Edit: The reason there is no GC happening is because your program finishes so quickly and consumes so little memory that there is no GC necessary. 编辑:没有发生GC的原因是因为您的程序完成得如此之快并且占用的内存很少,因此没有GC的必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM