Java虚拟机四 常用Java虚拟机参数

    主要涉及的知识点:

     1.跟踪Java虚拟机的垃圾回收和类加载等信息;

     2.配置Java虚拟机的堆空间;

     3.配置永久区和Java栈。

     4.学习虚拟机的服务器和客户端模式。

1.1 跟踪垃圾回收

    Java支持自动垃圾回收(GC),如果垃圾回收频繁出现,或者占用了太长的CPU时间,就必须重视起来。

    最简单的一个CG参数是-XX:+PrintGC,使用这个参数启动Java虚拟机后,只要遇到GC,就会打印日志。

    使用-XX:+PrintGCDetails参数,打印更加详细的信息。

[GC (System.gc()) [PSYoungGen: 8110K->792K(37888K)] 8110K->6944K(123904K), 0.0043826 secs] [Times: user=0.06 sys=0.02, real=0.00 secs] 
[Full GC (System.gc()) [PSYoungGen: 792K->0K(37888K)] [ParOldGen: 6152K->6806K(86016K)] 6944K->6806K(123904K), [Metaspace: 2669K->2669K(1056768K)], 0.0043210 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 PSYoungGen      total 37888K, used 328K [0x00000000d6000000, 0x00000000d8a00000, 0x0000000100000000)
  eden space 32768K, 1% used [0x00000000d6000000,0x00000000d6052030,0x00000000d8000000)
  from space 5120K, 0% used [0x00000000d8000000,0x00000000d8000000,0x00000000d8500000)
  to   space 5120K, 0% used [0x00000000d8500000,0x00000000d8500000,0x00000000d8a00000)
 ParOldGen       total 86016K, used 6806K [0x0000000082000000, 0x0000000087400000, 0x00000000d6000000)
  object space 86016K, 7% used [0x0000000082000000,0x00000000826a5970,0x0000000087400000)
 Metaspace       used 2676K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 286K, capacity 386K, committed 512K, reserved 1048576K

     详细信息描述了当前堆的各个区间的使用情况。

    通过使用参数-XX:+PrintHeapAtGC ,在每次GC前后分别打印堆的信息。

{Heap before GC invocations=1 (full 0):
 PSYoungGen      total 37888K, used 8110K [0x00000000d6000000, 0x00000000d8a00000, 0x0000000100000000)
  eden space 32768K, 24% used [0x00000000d6000000,0x00000000d67eb970,0x00000000d8000000)
  from space 5120K, 0% used [0x00000000d8500000,0x00000000d8500000,0x00000000d8a00000)
  to   space 5120K, 0% used [0x00000000d8000000,0x00000000d8000000,0x00000000d8500000)
 ParOldGen       total 86016K, used 0K [0x0000000082000000, 0x0000000087400000, 0x00000000d6000000)
  object space 86016K, 0% used [0x0000000082000000,0x0000000082000000,0x0000000087400000)
 Metaspace       used 2669K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 285K, capacity 386K, committed 512K, reserved 1048576K
Heap after GC invocations=1 (full 0):
 PSYoungGen      total 37888K, used 824K [0x00000000d6000000, 0x00000000d8a00000, 0x0000000100000000)
  eden space 32768K, 0% used [0x00000000d6000000,0x00000000d6000000,0x00000000d8000000)
  from space 5120K, 16% used [0x00000000d8000000,0x00000000d80ce010,0x00000000d8500000)
  to   space 5120K, 0% used [0x00000000d8500000,0x00000000d8500000,0x00000000d8a00000)
 ParOldGen       total 86016K, used 6152K [0x0000000082000000, 0x0000000087400000, 0x00000000d6000000)
  object space 86016K, 7% used [0x0000000082000000,0x0000000082602010,0x0000000087400000)
 Metaspace       used 2669K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 285K, capacity 386K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=2 (full 1):
 PSYoungGen      total 37888K, used 824K [0x00000000d6000000, 0x00000000d8a00000, 0x0000000100000000)
  eden space 32768K, 0% used [0x00000000d6000000,0x00000000d6000000,0x00000000d8000000)
  from space 5120K, 16% used [0x00000000d8000000,0x00000000d80ce010,0x00000000d8500000)
  to   space 5120K, 0% used [0x00000000d8500000,0x00000000d8500000,0x00000000d8a00000)
 ParOldGen       total 86016K, used 6152K [0x0000000082000000, 0x0000000087400000, 0x00000000d6000000)
  object space 86016K, 7% used [0x0000000082000000,0x0000000082602010,0x0000000087400000)
 Metaspace       used 2669K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 285K, capacity 386K, committed 512K, reserved 1048576K
Heap after GC invocations=2 (full 1):
 PSYoungGen      total 37888K, used 0K [0x00000000d6000000, 0x00000000d8a00000, 0x0000000100000000)
  eden space 32768K, 0% used [0x00000000d6000000,0x00000000d6000000,0x00000000d8000000)
  from space 5120K, 0% used [0x00000000d8000000,0x00000000d8000000,0x00000000d8500000)
  to   space 5120K, 0% used [0x00000000d8500000,0x00000000d8500000,0x00000000d8a00000)
 ParOldGen       total 86016K, used 6806K [0x0000000082000000, 0x0000000087400000, 0x00000000d6000000)
  object space 86016K, 7% used [0x0000000082000000,0x00000000826a5970,0x0000000087400000)
 Metaspace       used 2669K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 285K, capacity 386K, committed 512K, reserved 1048576K
}

       如果需要分析GC发生时间,可以使用-XX:+PrintGCTimeStamps参数,该参数会在每次GC发生时,额外输出GC发生的时间,输出的时间为虚拟机启动后的时间偏移量。

      由于GC会引起应用程序的停顿,因此,需要特别关注应用程序的执行时间和停顿时间。使用参数 -XX:+PrintGCApplicationConcurrentTime可以打印应用程序的执行时间,

使用参数 -XX:+PrintGCApplicationStoppedTime 可以打印应用程序由于GC而产生的停顿时间。

      跟踪系统内的软引用、弱引用、虚引用和Finallize队列,则可以使用-XX:PrintReferenceCG;

     使用参数 -Xloggc指定GC日志的输出文件,比如 -Xloggc:log/gc.log

 类加载/卸载的跟踪

     Java程序的运行离不开类的加载,为了更好的理解程序的执行,有时候需要知道系统加载了哪些类。一般情况下,系统加载的类存在于文件系统中,以jar的形式打包或者以class文件的形式存在,可以直接通过文件系统查看。

但是随着动态代理、AOP等技术的普遍使用,系统也极有可能在运行时动态生成某些类,这些类相对比较隐蔽,无法通过文件系统找到。

     使用参数 -verbose:class 跟踪类的加载和卸载。也可单独使用参数-XX:+TraceClassLoading跟踪类的加载,使用参数 -XX:+TraceClassUnloading 跟踪类的卸载。这两类参数是等价的。

     Java虚拟机还允许研发人员在运行时打印、查看系统中类的分布情况,只要在系统启动时加上 -XX:+PrintClassHistogram参数,然后在Java的控制台下Ctrl + Break 组合键,控制台上就会显示当前的类信息柱状图,可以看到占用空间最多的对象,以及其实例数量和空间大小。

原文地址:https://www.cnblogs.com/taiguyiba/p/9760727.html