Java8 默认垃圾回收器(GC)

一 概述

GC(Garbage Collection),在程序运行过程中内存空间是有限的,为了更好的的使用有限的内存空间,GC会将不再使用的对象清除然后将其所占用的内存释放出来。

触发GC的条件:

1. 程序调用System.gc的时候触发GC。

    /**
     * Runs the garbage collector.
     * 

* Calling the gc method suggests that the Java Virtual * Machine expend effort toward recycling unused objects in order to * make the memory they currently occupy available for quick reuse. * When control returns from the method call, the Java Virtual * Machine has made a best effort to reclaim space from all discarded * objects. *

* The call System.gc() is effectively equivalent to the * call: *

     * Runtime.getRuntime().gc()
     * 
* * @see java.lang.Runtime#gc() */ public static void gc() { Runtime.getRuntime().gc(); }

2. 系统自身决定GC的触发时机,主要根据Eden区和From Space区的内存大小来决定,当内存大小不足时,则会启动GC线程(Daemon线程)并停止应用线程。

          Java8 默认垃圾回收器(GC)_第1张图片

二 查看Java8的默认GC

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

java -XX:+PrintCommandLineFlags -version

结果如下:

-XX:InitialHeapSize=132397312 // JVM默认初始化堆大小
-XX:MaxHeapSize=2118356992 //JVM堆的默认最大值
-XX:+PrintCommandLineFlags 
-XX:+UseCompressedClassPointers 
-XX:+UseCompressedOops 
-XX:-UseLargePagesIndividualAllocation 
-XX:+UseParallelGC //Java8使用的GC类型
java version "1.8.0_20" //使用的java版本
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
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之间)来看是基于"标记—复制"算法实现,从整体来看是基于"标记-整理"算法实现】

2. cmd命令行查看Java8的GC详细情况:

java -XX:+PrintGCDetails -version

结果如下:

java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
Heap
 PSYoungGen      total 38400K, used 2678K [0x00000000d5e00000, 0x00000000d8880000, 0x0000000100000000)
  eden space 33280K, 8% used [0x00000000d5e00000,0x00000000d609dbc0,0x00000000d7e80000)
  from space 5120K, 0% used [0x00000000d8380000,0x00000000d8380000,0x00000000d8880000)
  to   space 5120K, 0% used [0x00000000d7e80000,0x00000000d7e80000,0x00000000d8380000)
 ParOldGen       total 87552K, used 0K [0x0000000081a00000, 0x0000000086f80000, 0x00000000d5e00000)
  object space 87552K, 0% used [0x0000000081a00000,0x0000000081a00000,0x0000000086f80000)
 Metaspace       used 2257K, capacity 4480K, committed 4480K, reserved 1056768K
  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

-XX:InitialHeapSize=132397312 
-XX:MaxHeapSize=2118356992 
-XX:ParallelGCThreads=4 
-XX:+PrintCommandLineFlags 
-XX:+UseCompressedOops 
-XX:-UseLargePagesIndividualAllocation 
-XX:+UseParallelGC
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)

java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
Heap
 PSYoungGen      total 37696K, used 646K [0x00000007d5e00000, 0x00000007d8810000, 0x0000000800000000)
  eden space 32320K, 2% used [0x00000007d5e00000,0x00000007d5ea19a8,0x00000007d7d90000)
  from space 5376K, 0% used [0x00000007d82d0000,0x00000007d82d0000,0x00000007d8810000)
  to   space 5376K, 0% used [0x00000007d7d90000,0x00000007d7d90000,0x00000007d82d0000)
 PSOldGen        total 86272K, used 0K [0x0000000781a00000, 0x0000000786e40000, 0x00000007d5e00000)
  object space 86272K, 0% used [0x0000000781a00000,0x0000000781a00000,0x0000000786e40000)
 PSPermGen       total 21248K, used 2709K [0x000000077c800000, 0x000000077dcc0000, 0x0000000781a00000)
  object space 21248K, 12% used [0x000000077c800000,0x000000077caa5738,0x000000077dcc0000)

JDK1.7

-XX:InitialHeapSize=132397312 
-XX:MaxHeapSize=2118356992 
-XX:+PrintCommandLineFlags 
-XX:+UseCompressedOops 
-XX:-UseLargePagesIndividualAllocation 
-XX:+UseParallelGC
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

//1.7中的PSPermGen永久代到1.8中是Metaspace
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
Heap
 PSYoungGen      total 38400K, used 1997K [0x00000007d5e00000, 0x00000007d8880000, 0x0000000800000000)
  eden space 33280K, 6% used [0x00000007d5e00000,0x00000007d5ff3480,0x00000007d7e80000)
  from space 5120K, 0% used [0x00000007d8380000,0x00000007d8380000,0x00000007d8880000)
  to   space 5120K, 0% used [0x00000007d7e80000,0x00000007d7e80000,0x00000007d8380000)
 ParOldGen       total 86016K, used 0K [0x0000000781a00000, 0x0000000786e00000, 0x00000007d5e00000)
  object space 86016K, 0% used [0x0000000781a00000,0x0000000781a00000,0x0000000786e00000)
 PSPermGen       total 21504K, used 2222K [0x000000077c800000, 0x000000077dd00000, 0x0000000781a00000)
  object space 21504K, 10% used [0x000000077c800000,0x000000077ca2b8c0,0x000000077dd00000)

JDK1.9

-XX:G1ConcRefinementThreads=4 
-XX:InitialHeapSize=132397312 
-XX:MaxHeapSize=2118356992 
-XX:+PrintCommandLineFlags 
-XX:ReservedCodeCacheSize=251658240 
-XX:+SegmentedCodeCache 
-XX:+UseCompressedClassPointers 
-XX:+UseCompressedOops 
-XX:+UseG1GC 
-XX:-UseLargePagesIndividualAllocation
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

C:\Users\pengu>java -Xlog:Gc* -version
[0.015s][info][gc,heap] Heap region size: 1M
[0.021s][info][gc     ] Using G1
[0.021s][info][gc,heap,coops] Heap address: 0x0000000081a00000, size: 2022 MB, Compressed Oops mode: 32-bit
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
[0.123s][info][gc,heap,exit ] Heap
[0.124s][info][gc,heap,exit ]  garbage-first heap   total 131072K, used 1024K [0x0000000081a00000, 0x0000000081b00400, 0x0000000100000000)
[0.124s][info][gc,heap,exit ]   region size 1024K, 2 young (2048K), 0 survivors (0K)
[0.125s][info][gc,heap,exit ]  Metaspace       used 3453K, capacity 4480K, committed 4480K, reserved 1056768K
[0.126s][info][gc,heap,exit ]   class space    used 352K, capacity 384K, committed 384K, reserved 1048576Kjava

 

你可能感兴趣的:(Java,JVM,JVM,GC,Metaspace,PermGenspace,Native,Memory)