eclipse设置查看GC日志和如何理解GC日志

2025-03-31 14:11:18
推荐回答(1个)
回答1:

1. Run as -> Run configurations -> java应用名 -> arguments ->VM arguments,加入jvm参数就行

2. 测试代码

[java] view plain copy

  • package cn.erong.test;  

  • public class Jtest {  

  • private static final int _1M = 1024*1024;  

  • public static void main(String[] args) {  

  • byte[] allocation1,allocation2,allocation3,allocation4;  

  • allocation1 = new byte[_1M/4];  

  • allocation2 = new byte[_1M/4];  

  • allocation3 = new byte[4*_1M];  

  • allocation4 = new byte[4*_1M];  

  • allocation4 = null;  

  • allocation4 = new byte[4*_1M];  

  • }  

  • }     


  • 3.  测试看下,在vm arguments 中加入

    [java] view plain copy

  • -Xms20m --jvm堆的最小值  

  • -Xmx20m --jvm堆的最大值  

  • -XX:+PrintGCTimeStamps -- 打印出GC的时间信息  

  • -XX:+PrintGCDetails  --打印出GC的详细信息  

  • -verbose:gc --开启gc日志  

  • -Xloggc:d:/gc.log -- gc日志的存放位置  

  • -Xmn10M -- 新生代内存区域的大小  

  • -XX:SurvivorRatio=8 --新生代内存区域中Eden和Survivor的比例  

  • 4 . run  看下日志,到d盘找到 gc.log,如下

    [plain] view plain copy

  • Java HotSpot(TM) Client VM (25.151-b12) for windows-x86 JRE (1.8.0_151-b12), built on Sep  5 2017 19:31:49 by "java_re" with MS VC++ 10.0 (VS2010)  

  • Memory: 4k page, physical 3567372k(982296k free), swap 7133056k(3042564k free)  

  • CommandLine flags: -XX:InitialHeapSize=20971520 -XX:MaxHeapSize=20971520 -XX:MaxNewSize=10485760 -XX:NewSize=10485760 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:SurvivorRatio=8 -XX:-UseLargePagesIndividualAllocation   

  • 0.091: [GC (Allocation Failure) 0.091: [DefNew: 5427K->995K(9216K), 0.0036445 secs] 5427K->5091K(19456K), 0.0038098 secs] [Times: user=0.00 sys=0.02, real=0.00 secs]   

  • 0.095: [GC (Allocation Failure) 0.095: [DefNew: 5091K->0K(9216K), 0.0012412 secs] 9187K->5090K(19456K), 0.0012908 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]   

  • Heap  

  • def new generation   total 9216K, used 4260K [0x04000000, 0x04a00000, 0x04a00000)  

  • eden space 8192K,  52% used [0x04000000, 0x044290e8, 0x04800000)  

  • from space 1024K,   0% used [0x04800000, 0x04800000, 0x04900000)  

  • to   space 1024K,   0% used [0x04900000, 0x04900000, 0x04a00000)  

  • tenured generation   total 10240K, used 5090K [0x04a00000, 0x05400000, 0x05400000)  

  • the space 10240K,  49% used [0x04a00000, 0x04ef8ac0, 0x04ef8c00, 0x05400000)  

  • Metaspace       used 84K, capacity 2242K, committed 2368K, reserved 4480K