JVM知识总结

212 阅读4分钟

-XX:+PrintGCDetails(重点)

作用:配置jvm此参数,能够打印gc的细节情况。 例子:

public class HelloGc {
    public static void main(String[] args) {
        System.out.println("*****hello gc");
    }
}

输出结果:

*****hello gc
Heap
 PSYoungGen      total 76288K, used 3932K [0x000000076ab00000, 0x0000000770000000, 0x00000007c0000000)
  eden space 65536K, 6% used [0x000000076ab00000,0x000000076aed7240,0x000000076eb00000)
  from space 10752K, 0% used [0x000000076f580000,0x000000076f580000,0x0000000770000000)
  to   space 10752K, 0% used [0x000000076eb00000,0x000000076eb00000,0x000000076f580000)
 ParOldGen       total 175104K, used 0K [0x00000006c0000000, 0x00000006cab00000, 0x000000076ab00000)
  object space 175104K, 0% used [0x00000006c0000000,0x00000006c0000000,0x00000006cab00000)
 Metaspace       used 2914K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 319K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

java 1.8后,堆空间组成:

  • PSYoungGen
  • ParOldGen
  • Metaspace

上述代码没有产生gc, 重新设置jvm参数:

//设置堆空间初始值为10m, 最大空间值也是10m
-Xms10m -Xmx10m -XX:+PrintGCDetails

运行以下代码:

public class HelloGc {
    public static void main(String[] args) {
        System.out.println("*****hello gc");
        byte[] bytes = new byte[50*1024*1024];
    }
}

结果如下:

*****hello gc
[GC (Allocation Failure) [PSYoungGen: 1307K->496K(2560K)] 1307K->528K(9728K), 0.0020844 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 496K->496K(2560K)] 528K->536K(9728K), 0.0012161 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 496K->0K(2560K)] [ParOldGen: 40K->367K(7168K)] 536K->367K(9728K), [Metaspace: 2908K->2908K(1056768K)], 0.0041615 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 367K->367K(9728K), 0.0009405 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 367K->350K(7168K)] 367K->350K(9728K), [Metaspace: 2908K->2908K(1056768K)], 0.0037563 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at com.company.jvm.HelloGc.main(HelloGc.java:6)
Heap
 PSYoungGen      total 2560K, used 80K [0x00000007bfd00000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2048K, 3% used [0x00000007bfd00000,0x00000007bfd143f8,0x00000007bff00000)
  from space 512K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007bff80000)
  to   space 512K, 0% used [0x00000007bff80000,0x00000007bff80000,0x00000007c0000000)
 ParOldGen       total 7168K, used 350K [0x00000007bf600000, 0x00000007bfd00000, 0x00000007bfd00000)
  object space 7168K, 4% used [0x00000007bf600000,0x00000007bf657970,0x00000007bfd00000)
 Metaspace       used 2953K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 322K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 1

GC的参数解读:

printGcDtails

Full GC的参数解读

full gc