图片


之前写过一个例子,展示对象循环引用仍然会被 JVM 的垃圾收集器回收。

代码如下:

package constxiong.jvm.gc;

/**
 * 引用计数
 */

public class ReferenceCount {

    public ReferenceCount holder;

    /**
     * 占用 5 M
     */

    private byte[] appendMemory = new byte[5 * 1024 * 1024];

    public static void main(String[] args{
        ReferenceCount obj1 = new ReferenceCount();
        ReferenceCount obj2 = new ReferenceCount();
        obj1.holder = obj2;//obj1 持有 obj2 对象
        obj2.holder = obj1;//obj2 持有 obj1 对象

        obj1 = null;
        obj2 = null;

        System.gc();
    }

}


如果希望看到这段代码执行过程中,GC 的执行与内存回收情况,需要给 java 命令加上参数,开启 GC 日志,这样可以辅助排查 JVM 出现的问题。


我本地电脑环境是 64 位 win7,JDK 8


1、查看 GC 基本信息

参数:

-XX:+PrintGC

执行打印结果:

[GC (System.gc())  12868K->768K(123904K), 0.0223098 secs]
[Full GC (System.gc())  768K->613K(123904K), 0.0226187 secs]


2、查看 GC 详细信息

参数:

-XX:+PrintGCDetails

执行打印结果:

[GC (System.gc()) [PSYoungGen: 13523K->792K(37888K)] 13523K->800K(123904K), 0.0302122 secs] [Times: user=0.08 sys=0.00real=0.03 secs] 
[Full GC (System.gc()) [PSYoungGen: 792K->0K(37888K)] [ParOldGen: 8K->650K(86016K)] 800K->650K(123904K), [Metaspace: 3347K->3347K(1056768K)], 0.0181992 secs] [Times: user=0.06 sys=0.02real=0.02 secs] 
Heap
 PSYoungGen      total 37888K, used 983K [0x00000000d6100000, 0x00000000d8b00000, 0x0000000100000000)
  eden space 32768K, 3% used [0x00000000d6100000,0x00000000d61f5db8,0x00000000d8100000)
  from space 5120K, 0% used [0x00000000d8100000,0x00000000d8100000,0x00000000d8600000)
  to   space 5120K, 0% used [0x00000000d8600000,0x00000000d8600000,0x00000000d8b00000)
 ParOldGen       total 86016K, used 650K [0x00000000822000000x00000000876000000x00000000d6100000)
  object space 86016K, 0% used [0x0000000082200000,0x00000000822a2a68,0x0000000087600000)
 Metaspace       used 3354K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 371K, capacity 388K, committed 512K, reserved 1048576K


3、查看 GC 前后的堆、方法区可用容量变化

参数:

-XX:+PrintHeapAtGC
执行打印结果:
{Heap before GC invocations=1 (full 0):
 PSYoungGen      total 37888K, used 13523K [0x00000000d6100000, 0x00000000d8b00000, 0x0000000100000000)
  eden space 32768K, 41% used [0x00000000d6100000,0x00000000d6e34f90,0x00000000d8100000)
  from space 5120K, 0% used [0x00000000d8600000,0x00000000d8600000,0x00000000d8b00000)
  to   space 5120K, 0% used [0x00000000d8100000,0x00000000d8100000,0x00000000d8600000)
 ParOldGen       total 86016K, used 0K [0x00000000822000000x00000000876000000x00000000d6100000)
  object space 86016K, 0% used [0x0000000082200000,0x0000000082200000,0x0000000087600000)
 Metaspace       used 3437K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 376K, capacity 388K, committed 512K, reserved 1048576K
Heap after GC invocations=1 (full 0):
 PSYoungGen      total 37888K, used 744K [0x00000000d6100000, 0x00000000d8b00000, 0x0000000100000000)
  eden space 32768K, 0% used [0x00000000d6100000,0x00000000d6100000,0x00000000d8100000)
  from space 5120K, 14% used [0x00000000d8100000,0x00000000d81ba020,0x00000000d8600000)
  to   space 5120K, 0% used [0x00000000d8600000,0x00000000d8600000,0x00000000d8b00000)
 ParOldGen       total 86016K, used 8K [0x00000000822000000x00000000876000000x00000000d6100000)
  object space 86016K, 0% used [0x0000000082200000,0x0000000082202000,0x0000000087600000)
 Metaspace       used 3437K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 376K, capacity 388K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=2 (full 1):
 PSYoungGen      total 37888K, used 744K [0x00000000d6100000, 0x00000000d8b00000, 0x0000000100000000)
  eden space 32768K, 0% used [0x00000000d6100000,0x00000000d6100000,0x00000000d8100000)
  from space 5120K, 14% used [0x00000000d8100000,0x00000000d81ba020,0x00000000d8600000)
  to   space 5120K, 0% used [0x00000000d8600000,0x00000000d8600000,0x00000000d8b00000)
 ParOldGen       total 86016K, used 8K [0x00000000822000000x00000000876000000x00000000d6100000)
  object space 86016K, 0% used [0x0000000082200000,0x0000000082202000,0x0000000087600000)
 Metaspace       used 3437K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 376K, capacity 388K, committed 512K, reserved 1048576K
Heap after GC invocations=2 (full 1):
 PSYoungGen      total 37888K, used 0K [0x00000000d6100000, 0x00000000d8b00000, 0x0000000100000000)
  eden space 32768K, 0% used [0x00000000d6100000,0x00000000d6100000,0x00000000d8100000)
  from space 5120K, 0% used [0x00000000d8100000,0x00000000d8100000,0x00000000d8600000)
  to   space 5120K, 0% used [0x00000000d8600000,0x00000000d8600000,0x00000000d8b00000)
 ParOldGen       total 86016K, used 636K [0x00000000822000000x00000000876000000x00000000d6100000)
  object space 86016K, 0% used [0x0000000082200000,0x000000008229f220,0x0000000087600000)
 Metaspace       used 3437K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 376K, capacity 388K, committed 512K, reserved 1048576K}


4、查看 GC 过程中用户线程并发时间以及停顿的时间参数:

-XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCApplicationStoppedTime
执行打印结果:
Application time: 0.0043578 seconds
Total time for which application threads were stopped: 0.0092080 secondsStopping threads took: 0.0000222 seconds
Application time: 0.0028967 seconds


5、查看收集器自动设置堆空间各分代区域大小、收集目标等自动调节的相关信息

参数:

-XX:+PrintAdaptiveSizePolicy

执行打印结果:

AdaptiveSizePolicy::update_averages:  survived: 778288  promoted: 8192  overflow: false
AdaptiveSizeStart: 0.134 collection: 1 
AdaptiveSizeStop: collection: 1 
AdaptiveSizeStart: 0.138 collection: 2 
AdaptiveSizeStop: collection: 2 


6、查看熬过收集后剩余对象的年龄分布信息

参数:

-XX:+PrintTenuringDistribution

执行打印结果:

Desired survivor size 5242880 bytes, new threshold 7 (max 15)


7、查看 jdk jvm 全量参数设置

命令:

java -XX:+PrintFlagsFinal

执行打印结果列出了 JVM 所有参数

[Global flags]
    uintx AdaptiveSizeDecrementScaleFactor          = 4                                   {product}
    uintx AdaptiveSizeMajorGCDecayTimeScale         = 10                                  {product}
    .
    .
    .
     bool ZeroTLAB                                  = false                               {product}
     intx hashCode                                  = 5                                   {product}

JDK 9 开始,部分参数是有所变动的。


详细的参数列表可以查看官方说明:

  • https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html

  • https://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html

  • https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html

  • https://www.cnblogs.com/chenjack/p/12711214.html这篇中文翻译整理的全些)


下篇可以把常用配置参数拎出来,整理成表格。


更多相关文章

  1. HotSpot VM 中对象的内存分析
  2. 从对象生命周期的经验统计到垃圾回收算法
  3. 什么样的 Java 对象会被当垃圾回收?
  4. 构造方法的参数太多,如何解决?
  5. 从代码的改进,看参数行为化与Lambda
  6. Spring Ioc 实例化 Bean 对象有几种方式?
  7. PHP-FPM参数调优
  8. Javascript面向对象入门
  9. springboot 读写 session 交互参数

随机推荐

  1. Android(安卓)进阶学习:事件分发机制完全
  2. Android(安卓)进阶之刁钻问题汇总
  3. Android(安卓)驱动跟系统开发 1. 一个简
  4. Android(安卓)Menu的应用
  5. Android(安卓)ConstraintLayout 使用
  6. Android性能监测小工具——安测试
  7. Android(安卓)中的 Service 全面总结
  8. android EditText限制输入长度
  9. Android初学者教程
  10. Android——横屏和竖屏的切换,以及明文密