JVM 垃圾回收日誌 與 算法深度解讀

GC的時機
在分代模型的基礎上,GC從時機上分爲兩種:Scavenge GC 和 Full GC

Scavenge GC (Mionr GC)
觸發時機:新對象生成時,Eden空間滿了
理論上Eden區大多數對象會在Scavenge GC 回收,複製算法的執行效率會很高,Scavenge GC時間比較短

Full GC
整個jvm進行整理,包括Young、old和perm
主要觸發時機:old滿了 perm滿了 system.gc()
效率很低,儘量減少Full GC

配置jvm 參數如下:
-verbose:gc 打印垃圾收集的情況
-Xms20M  最小堆大小
-Xmx20M  最大堆大小
-Xmn10M  新生代大小
-XX:+PrintGCDetails  打印GC收集詳細信息
-XX:SurvivorRatio=8  eden 與 from survivor to survivor 比例 8:1:1
public static void main(String[] args) {
        int size = 1024 * 1024;
        byte[] myAlloc1 = new byte[2*size];
        byte[] myAlloc2 = new byte[2*size];
        byte[] myAlloc3 = new byte[2*size];
        byte[] myAlloc4 = new byte[2*size];
        System.out.println("Test1.main");
}

打印結果:

 GC (Allocation Failure): Eden滿了 分配失敗進行GC垃圾回收

 PSYoungGen: 表述 使用parallel Scavenge 收集器

 ParOldGen:表示 parallel old 老年代收集器

 6874K->992K(9216K) : 6874K表示回收之前存活對象在新生代的空間的容量,992K表示回收後存活對象的容量

 9216K:表示新生代總空間容量大小

 6874K->3337K(19456K):6874K表示對象總的堆大小,3337K執行完回收之後 存活對象總的堆大的大小

 PSYoungGen:表示新生代

 ParOldGen :老年代

 6874-992 = 5882 //執行完GC後,新生代釋放的空間的容量,新生代釋放的空間 :1)被回收 2)晉升到老年代

 6874-3337=3537 //執行完GC後,總的堆空間釋放的容量

 5882-3537=2345 //等於ParOldGen 老年代容量

[GC (Allocation Failure) [PSYoungGen: 6874K->992K(9216K)] 6874K->3337K(19456K), 0.0042664 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Test1.main
Heap
 PSYoungGen      total 9216K, used 7458K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 78% used [0x00000000ff600000,0x00000000ffc50848,0x00000000ffe00000)
  from space 1024K, 96% used [0x00000000ffe00000,0x00000000ffef8188,0x00000000fff00000)
  to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
 ParOldGen       total 10240K, used 2345K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 22% used [0x00000000fec00000,0x00000000fee4a608,0x00000000ff600000)
 Metaspace       used 3304K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 359K, capacity 388K, committed 512K, reserved 1048576K

 

 public static void main(String[] args) {
        int size = 1024 * 1024;
        byte[] myAlloc1 = new byte[2*size];
        byte[] myAlloc2 = new byte[2*size];
        byte[] myAlloc3 = new byte[2*size];
        byte[] myAlloc4 = new byte[2*size];
        byte[] myAlloc5 = new byte[2*size];
        System.out.println("Test1.main");
    }

打印結果:

 Full GC (Ergonomics):會帶來stop the world 讓業務線程暫停

 PSYoungGen: 1008K->0K(9216K):新生代回收後爲0

 ParOldGen: 8513K->9157K(10240K):老年代回收後,容量變多了,因爲新生代一部分數據進入老年代


[GC (Allocation Failure) [PSYoungGen: 6874K->1016K(9216K)] 6874K->3361K(19456K), 0.0040588 secs] [Times: user=0.03 sys=0.02, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 7315K->1008K(9216K)] 9661K->9521K(19456K), 0.0063014 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[Full GC (Ergonomics) [PSYoungGen: 1008K->0K(9216K)] [ParOldGen: 8513K->9157K(10240K)] 9521K->9157K(19456K), [Metaspace: 3296K->3296K(1056768K)], 0.0090750 secs] [Times: user=0.03 sys=0.00, real=0.01 secs] 
Test1.main
Heap
 PSYoungGen      total 9216K, used 2270K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 27% used [0x00000000ff600000,0x00000000ff837838,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 9157K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 89% used [0x00000000fec00000,0x00000000ff4f1430,0x00000000ff600000)
 Metaspace       used 3304K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 359K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0

 

 

    public static void main(String[] args) {
        int size = 1024 * 1024;
        byte[] myAlloc1 = new byte[8*size];
        System.out.println("Test1.main");
    }

打印結果:

 虛擬機分配過程,當對象創建時先進入新生代空間,當新生代空間滿時,新創建的對象將直接進入到老年代。

Test1.main
Heap
 PSYoungGen      total 9216K, used 4992K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 60% used [0x00000000ff600000,0x00000000ffae0370,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 8192K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 80% used [0x00000000fec00000,0x00000000ff400010,0x00000000ff600000)
 Metaspace       used 3302K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 359K, capacity 388K, committed 512K, reserved 1048576K

 

java -XX:+PrintCommandLineFlags -version 打印jvm啓動參數以及java版本號

java -XX:+PrintCommandLineFlags -version
-XX:InitialHeapSize=132269760 -XX:MaxHeapSize=2116316160 -XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndiv
idualAllocation -XX:+UseParallelGC
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b15, mixed mode)

InitialHeapSize 等價於

Xms  MaxHeapSize 等價於 Xmx   

默認GC UseParallelGC : 1) parallel old 老年代收集器 2)parallel Scavenge 新生代收集器

 

配置jvm 參數如下:

-verbose:gc
-Xms20M
-Xmx20M
-Xmn10M
-XX:+PrintGCDetails
-XX:SurvivorRatio=8
-XX:PretenureSizeThreshold=4194304 當我們創建的對象大小,超過指定的大小,那麼它不會在新生代中分配,而是直接去老年代中分配 並且它需要和 串行收集器 一起使用纔會生效
-XX:UseSerialGC

    public static void main(String[] args) {
        int size = 1024 * 1024;
        byte[] myAlloc1 = new byte[5*size];
        System.out.println("Test1.main");
    }

打印結果:

 可以看出我們配置PretenureSizeThreshold:4M大小,創建一個5M對象,並且在串行收集器下,直接分配到老年代中

Test2.main
Heap
 def new generation   total 9216K, used 4992K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  eden space 8192K,  60% used [0x00000000fec00000, 0x00000000ff0e0370, 0x00000000ff400000)
  from space 1024K,   0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
  to   space 1024K,   0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)
 tenured generation   total 10240K, used 5120K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
   the space 10240K,  50% used [0x00000000ff600000, 0x00000000ffb00010, 0x00000000ffb00200, 0x0000000100000000)
 Metaspace       used 3302K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 359K, capacity 388K, committed 512K, reserved 1048576K

 

配置jvm 參數如下:

* -verbose:gc
* -Xms20M
* -Xmx20M
* -Xmn10M
* -XX:+PrintGCDetails
* -XX:SurvivorRatio=8
* -XX:MaxTenuringThreshold=5
* -XX:+PrintTenuringDistribution
 MaxTenuringThreshold 作用:在可以自動調節對象晉升到老年代閾值的gc中,設置該閾值的最大值;該參數的默認值爲15,cms中默認值爲6,g1中默認值爲15
經歷了多次gc後,存活的對象會在form survivor 與 to survivor 之間來回存放,而這裏面的一個前提是這兩個空間有足夠的大小來存放這些數據,在gc算法中,會計算每個對象年齡的大小,如果達到某個年齡後發現總大小已經大於了survivor空間的50%,那麼這時就需要調整閾值,不能等到默認的15次
才完成晉升,這樣會導致survivor空間不足,所以需要調整閾值,讓這些存活對象儘快完成晉升

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章