一 概述

GC(Garbage Collection),在程序运行过程中内存空间是有限的,为了更好的的使用有限的内存空间,GC会将不再使用的对象清除然后将其所占用的内存释放出来。
触发GC的条件:
1. 程序调用System.gc的时候触发GC。

  1. /**
  2. * Runs the garbage collector.
  3. * <p>
  4. * Calling the <code>gc</code> method suggests that the Java Virtual
  5. * Machine expend effort toward recycling unused objects in order to
  6. * make the memory they currently occupy available for quick reuse.
  7. * When control returns from the method call, the Java Virtual
  8. * Machine has made a best effort to reclaim space from all discarded
  9. * objects.
  10. * <p>
  11. * The call <code>System.gc()</code> is effectively equivalent to the
  12. * call:
  13. * <blockquote><pre>
  14. * Runtime.getRuntime().gc()
  15. * </pre></blockquote>
  16. *
  17. * @see java.lang.Runtime#gc()
  18. */
  19. public static void gc() {
  20. Runtime.getRuntime().gc();
  21. }
  1. 系统自身决定GC的触发时机,主要根据Eden区和From Space区的内存大小来决定,当内存大小不足时,则会启动GC线程(Daemon线程)并停止应用线程。
    image.png

二 查看Java8的默认GC

1. cmd命令行查看Java8的GC:

  1. java -XX:+PrintCommandLineFlags -version

结果如下:

  1. -XX:InitialHeapSize=132397312 // JVM默认初始化堆大小
  2. -XX:MaxHeapSize=2118356992 //JVM堆的默认最大值
  3. -XX:+PrintCommandLineFlags
  4. -XX:+UseCompressedClassPointers
  5. -XX:+UseCompressedOops
  6. -XX:-UseLargePagesIndividualAllocation
  7. -XX:+UseParallelGC //Java8使用的GC类型
  8. java version "1.8.0_20" //使用的java版本
  9. Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
  10. Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

结果分析:由结果可以看出Java8的GC情况是:-XX:+UseParallelGC,即Parallel Scavenge(新生代) + Parallel Old(老生代),实际上几个主流Java版本的GC情况如下:

jdk1.7 默认垃圾收集器Parallel Scavenge(新生代【标记-复制算法】)+Parallel Old(老年代【标记整理算法】)
jdk1.8 默认垃圾收集器Parallel Scavenge(新生代)+Parallel Old(老年代)
jdk1.9 默认垃圾收集器G1【从局部(两个Region之间)来看是基于”标记—复制”算法实现,从整体来看是基于”标记-整理”算法实现】

  1. cmd命令行查看Java8的GC详细情况:
    1. java -XX:+PrintGCDetails -version
    结果如下:
    1. java version "1.8.0_20"
    2. Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
    3. Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
    4. Heap
    5. PSYoungGen total 38400K, used 2678K [0x00000000d5e00000, 0x00000000d8880000, 0x0000000100000000)
    6. eden space 33280K, 8% used [0x00000000d5e00000,0x00000000d609dbc0,0x00000000d7e80000)
    7. from space 5120K, 0% used [0x00000000d8380000,0x00000000d8380000,0x00000000d8880000)
    8. to space 5120K, 0% used [0x00000000d7e80000,0x00000000d7e80000,0x00000000d8380000)
    9. ParOldGen total 87552K, used 0K [0x0000000081a00000, 0x0000000086f80000, 0x00000000d5e00000)
    10. object space 87552K, 0% used [0x0000000081a00000,0x0000000081a00000,0x0000000086f80000)
    11. Metaspace used 2257K, capacity 4480K, committed 4480K, reserved 1056768K
    12. class space used 244K, capacity 384K, committed 384K, reserved 1048576K
    在Java中使用Metaspace(元空间)而移除了PermGenspace(永久区)。也就意味着这部分内存空间将全部移除。设置JVM的参数PermSize和MaxPermSize时会被忽略并给出警告。但是类的元数据信息(metadata)还在,只是不再存储在连续的堆空间中,而是移动到了被称为”Metaspace(元空间)”的本地内存(Native memory)中。

补充JDK1.6,JDK1.7与JDK1.9的信息:
JDK1.6

  1. -XX:InitialHeapSize=132397312
  2. -XX:MaxHeapSize=2118356992
  3. -XX:ParallelGCThreads=4
  4. -XX:+PrintCommandLineFlags
  5. -XX:+UseCompressedOops
  6. -XX:-UseLargePagesIndividualAllocation
  7. -XX:+UseParallelGC
  8. java version "1.6.0_43"
  9. Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
  10. Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
  11. java version "1.6.0_43"
  12. Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
  13. Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
  14. Heap
  15. PSYoungGen total 37696K, used 646K [0x00000007d5e00000, 0x00000007d8810000, 0x0000000800000000)
  16. eden space 32320K, 2% used [0x00000007d5e00000,0x00000007d5ea19a8,0x00000007d7d90000)
  17. from space 5376K, 0% used [0x00000007d82d0000,0x00000007d82d0000,0x00000007d8810000)
  18. to space 5376K, 0% used [0x00000007d7d90000,0x00000007d7d90000,0x00000007d82d0000)
  19. PSOldGen total 86272K, used 0K [0x0000000781a00000, 0x0000000786e40000, 0x00000007d5e00000)
  20. object space 86272K, 0% used [0x0000000781a00000,0x0000000781a00000,0x0000000786e40000)
  21. PSPermGen total 21248K, used 2709K [0x000000077c800000, 0x000000077dcc0000, 0x0000000781a00000)
  22. object space 21248K, 12% used [0x000000077c800000,0x000000077caa5738,0x000000077dcc0000)

JDK1.7

  1. -XX:InitialHeapSize=132397312
  2. -XX:MaxHeapSize=2118356992
  3. -XX:+PrintCommandLineFlags
  4. -XX:+UseCompressedOops
  5. -XX:-UseLargePagesIndividualAllocation
  6. -XX:+UseParallelGC
  7. java version "1.7.0_80"
  8. Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
  9. Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
  10. //1.7中的PSPermGen永久代到1.8中是Metaspace
  11. java version "1.7.0_80"
  12. Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
  13. Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
  14. Heap
  15. PSYoungGen total 38400K, used 1997K [0x00000007d5e00000, 0x00000007d8880000, 0x0000000800000000)
  16. eden space 33280K, 6% used [0x00000007d5e00000,0x00000007d5ff3480,0x00000007d7e80000)
  17. from space 5120K, 0% used [0x00000007d8380000,0x00000007d8380000,0x00000007d8880000)
  18. to space 5120K, 0% used [0x00000007d7e80000,0x00000007d7e80000,0x00000007d8380000)
  19. ParOldGen total 86016K, used 0K [0x0000000781a00000, 0x0000000786e00000, 0x00000007d5e00000)
  20. object space 86016K, 0% used [0x0000000781a00000,0x0000000781a00000,0x0000000786e00000)
  21. PSPermGen total 21504K, used 2222K [0x000000077c800000, 0x000000077dd00000, 0x0000000781a00000)
  22. object space 21504K, 10% used [0x000000077c800000,0x000000077ca2b8c0,0x000000077dd00000)

JDK1.9

  1. -XX:G1ConcRefinementThreads=4
  2. -XX:InitialHeapSize=132397312
  3. -XX:MaxHeapSize=2118356992
  4. -XX:+PrintCommandLineFlags
  5. -XX:ReservedCodeCacheSize=251658240
  6. -XX:+SegmentedCodeCache
  7. -XX:+UseCompressedClassPointers
  8. -XX:+UseCompressedOops
  9. -XX:+UseG1GC
  10. -XX:-UseLargePagesIndividualAllocation
  11. java version "9.0.4"
  12. Java(TM) SE Runtime Environment (build 9.0.4+11)
  13. Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
  14. C:\Users\pengu>java -Xlog:Gc* -version
  15. [0.015s][info][gc,heap] Heap region size: 1M
  16. [0.021s][info][gc ] Using G1
  17. [0.021s][info][gc,heap,coops] Heap address: 0x0000000081a00000, size: 2022 MB, Compressed Oops mode: 32-bit
  18. java version "9.0.4"
  19. Java(TM) SE Runtime Environment (build 9.0.4+11)
  20. Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
  21. [0.123s][info][gc,heap,exit ] Heap
  22. [0.124s][info][gc,heap,exit ] garbage-first heap total 131072K, used 1024K [0x0000000081a00000, 0x0000000081b00400, 0x0000000100000000)
  23. [0.124s][info][gc,heap,exit ] region size 1024K, 2 young (2048K), 0 survivors (0K)
  24. [0.125s][info][gc,heap,exit ] Metaspace used 3453K, capacity 4480K, committed 4480K, reserved 1056768K
  25. [0.126s][info][gc,heap,exit ] class space used 352K, capacity 384K, committed 384K, reserved 1048576Kjava

原文

https://blog.csdn.net/calm_encode/article/details/106621152