简体   繁体   中英

How does JVM print GC time?

My java code:

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)

I don't how much time GC costs.

The reason you do not see any GC time is because there is no GC happening when running your program. 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.

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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