模拟频繁YoungGC

案例

  1. public static void main(String[] args) {
  2. byte[] array1 = new byte[1024 * 1024];
  3. array1 = new byte[1024 * 1024];
  4. array1 = new byte[1024 * 1024];
  5. array1 = null;
  6. byte[] array2 = new byte[2 * 1024 * 1024];
  7. }

GC参数 堆10MB,新生代5MB

  1. -XX:NewSize=5242880 -XX:MaxNewSize=5242880 -XX:InitialHeapSize=10485760 -XX:MaxHeapSize=10485760 -XX:SurvivorRatio=8 -XX:PretenureSizeThreshold=10485760 -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:gc.log

GC日志

  1. Java HotSpot(TM) 64-Bit Server VM (25.291-b10) for windows-amd64 JRE (1.8.0_291-b10), built on Apr 9 2021 00:02:00 by "java_re" with MS VC++ 15.9 (VS2017)
  2. Memory: 4k page, physical 8085072k(2061632k free), swap 22765136k(11078656k free)
  3. //代表采用什么参数
  4. CommandLine flags: -XX:InitialHeapSize=10485760 -XX:MaxHeapSize=10485760 -XX:MaxNewSize=5242880 -XX:NewSize=5242880 -XX:OldPLABSize=16
  5. -XX:PretenureSizeThreshold=10485760 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:SurvivorRatio=8
  6. -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:-UseLargePagesIndividualAllocation -XX:+UseParNewGC
  7. //概要说明这次GC的执行情况
  8. 0.428(多少秒之后发生了GC): [GC (Allocation Failure) 0.428:
  9. //用了parNew也就是yougGC
  10. [ParNew: 3269K->512K(4608K)//总使用量是3269(不止包含了数组信息还有别的一些对象信息等)清理完成只有512k的对象存活下来 ,
  11. 0.0017316 secs //本次GC花费的时间] 3269K->1753K(9728K) //java堆总大小是9728 Eden区加老年代大小 ,GC之后Java堆内存使用了1753K。, 0.0019641 secs]
  12. //本次gc消耗的时间
  13. [Times: user=0.00 sys=0.00, real=0.00 secs]
  14. //GC过后堆的使用情况
  15. Heap
  16. //parNew负责的年轻代总过4608k,目前使用了3705k,在GC结束后又放了一个2MB array2进去了
  17. par new generation total 4608K, used 3705K [0x00000000ff600000, 0x00000000ffb00000, 0x00000000ffb00000)
  18. //Eden占用比例 此时那个2Mb的对象在里面
  19. eden space 4096K, 77% used [0x00000000ff600000, 0x00000000ff91e4c8, 0x00000000ffa00000)
  20. //Survivor区存活的对象
  21. from space 512K, 100% used [0x00000000ffa80000, 0x00000000ffb00000, 0x00000000ffb00000)
  22. to space 512K, 0% used [0x00000000ffa00000, 0x00000000ffa00000, 0x00000000ffa80000)
  23. //老年代信息
  24. concurrent mark-sweep generation total 5120K, used 1241K [0x00000000ffb00000, 0x0000000100000000, 0x0000000100000000)
  25. //元空间信息
  26. Metaspace used 2677K, capacity 4486K, committed 4864K, reserved 1056768K
  27. //类元信息
  28. class space used 284K, capacity 386K, committed 512K, reserved 1048576K

模拟进入老年代

案例

一次YoungGC

GC参数

  1. -XX:NewSize=10485760
  2. -XX:MaxNewSize=10485760
  3. -XX:InitialHeapSize=20971520
  4. -XX:MaxHeapSize=20971520
  5. -XX:SurvivorRatio=8
  6. -XX:MaxTenuringThreshold=15
  7. -XX:PretenureSizeThreshold=10485760
  8. -XX:+UseParNewGC
  9. -XX:+UseConcMarkSweepGC
  10. -XX:+PrintGCDetails
  11. -XX:+PrintGCTimeStamps
  12. -Xloggc:gc.log
  1. public static void main(String[] args) {
  2. byte[] array1 = new byte[2 * 1024 * 1024];
  3. array1 = new byte[2 * 1024 * 1024];
  4. array1 = new byte[2 * 1024 * 1024];
  5. array1 = null;
  6. byte[] array2 = new byte[128 * 1024];
  7. byte[] array3 = new byte[2 * 1024 * 1024];
  8. }

GC日志

  1. 0.853: [GC (Allocation Failure) 0.853:
  2. //存了array1的三个对象 6mb,
  3. //随后加入128k的数组array2 现在已经占了6272k了 array3必然存不下
  4. //存不下就触发GC,724k的存活对象被放入survivor中
  5. [ParNew: 6258K->724K(9216K), 0.0025129 secs]
  6. //随后又加入了2Mb的array3
  7. 6258K->2774K(19456K), 0.0029577 secs]
  8. [Times: user=0.00 sys=0.00, real=0.00 secs]
  9. Heap
  10. par new generation total 9216K, used 5185K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  11. eden space 8192K, 54% used [0x00000000fec00000, 0x00000000ff05b4b8, 0x00000000ff400000)
  12. from space 1024K, 70% used [0x00000000ff500000, 0x00000000ff5b5140, 0x00000000ff600000)
  13. to space 1024K, 0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
  14. concurrent mark-sweep generation total 10240K, used 2050K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  15. Metaspace used 3311K, capacity 4496K, committed 4864K, reserved 1056768K
  16. class space used 359K, capacity 388K, committed 512K, reserved 1048576K

二次YoungGC

  1. public static void main(String[] args) {
  2. byte[] array1 = new byte[2 * 1024 * 1024];
  3. array1 = new byte[2 * 1024 * 1024];
  4. array1 = new byte[2 * 1024 * 1024];
  5. array1 = null;
  6. byte[] array2 = new byte[128 * 1024];
  7. byte[] array3 = new byte[2 * 1024 * 1024];
  8. array3 = new byte[2 * 1024 * 1024];
  9. array3 = new byte[2 * 1024 * 1024];
  10. array3 = new byte[128 * 1024];
  11. array3 = null;
  12. byte[] array4 = new byte[2 * 1024 * 1024];
  13. }

gc日志

  1. 0.499: [GC (Allocation Failure) 0.499: [ParNew: 6786K->771K(9216K), 0.0013279 secs] 6786K->2821K(19456K), 0.0015758 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
  2. 0.502: [GC (Allocation Failure) 0.502: [ParNew: 7365K->128K(9216K), 0.0020625 secs] 9415K->4896K(19456K), 0.0021589 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
  3. Heap
  4. par new generation total 9216K, used 4590K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  5. eden space 8192K, 54% used [0x00000000fec00000, 0x00000000ff05b988, 0x00000000ff400000)
  6. from space 1024K, 12% used [0x00000000ff400000, 0x00000000ff420010, 0x00000000ff500000)
  7. to space 1024K, 0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
  8. concurrent mark-sweep generation total 10240K, used 4768K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  9. Metaspace used 3097K, capacity 4556K, committed 4864K, reserved 1056768K
  10. class space used 324K, capacity 392K, committed 512K, reserved 1048576K

JSTAT分析

分析线上系统jvm运行

1.Eden区的对象增长速率多块?Young GC频率多高?
2.一次Young GC多长耗时?
3.Young GC过后多少对象存活?
4.老年代的对象增长速率多高?
5.Full GC频率多高?
6.一次Full GC耗时?