天天看點

JVM參數 JAVA垃圾收集器之ParNew收集器jVM堆記憶體相關的啟動參數:年輕代、老年代和永久代的記憶體配置設定

https://www.cnblogs.com/itboys/p/7227893.html?utm_source=itdadao&utm_medium=referral

jVM堆記憶體相關的啟動參數:年輕代、老年代和永久代的記憶體配置設定

//常見配置彙總

//堆設定

-Xms:初始堆大小

-Xmx:最大堆大小

-XX:NewSize=n:設定年輕代大小

-XX:NewRatio=n:設定年輕代和年老代的比值.如:為3,表示年輕代與年老代比值為1:3,年輕代占整個年輕代年老代和的1/4

-XX:SurvivorRatio=n:年輕代中Eden區與兩個Survivor區的比值.注意Survivor區有兩個.如:3,表示Eden:Survivor=3:2,一個Survivor區占整個年輕代的1/5

-XX:MaxPermSize=n:設定持久代大小

//收集器設定

-XX:+UseSerialGC:設定串行收集器

-XX:+UseParallelGC:設定并行收集器

-XX:+UseParalledlOldGC:設定并行年老代收集器

-XX:+UseConcMarkSweepGC:設定并發收集器

//垃圾回收統計資訊

-XX:+PrintGC

-XX:+PrintGCDetails

-XX:+PrintGCTimeStamps

-Xloggc:filename

//并行收集器設定

-XX:ParallelGCThreads=n:設定并行收集器收集時使用的CPU數.并行收集//線程數.

-XX:MaxGCPauseMillis=n:設定并行收集最大暫停時間

-XX:GCTimeRatio=n:設定垃圾回收時間占程式運作時間的百分比.公式為1/(1+n)

//并發收集器設定

-XX:+CMSIncrementalMode:設定為增量模式.适用于單CPU情況.

-XX:ParallelGCThreads=n:設定并發收集器年輕代收集方式為并行收集時,使用的CPU數.并行收集線程數.

1、特點

ParNew收集器是JAVA虛拟機中垃圾收集器的一種。它是Serial收集器的多線程版本,除了使用多條線程進行垃圾收集之外,其餘行為包括Serial收集器可用的所有控制參數(例如:-XX:SurvivorRatio、-XX:PretenureSizeThreshold、-XX:HandlePromotionFailure等)、收集算法、Stop The World、對象配置設定規則、回收政策等都與Serial收集器一緻。

2、現狀

ParNew是許多運作在Server模式下的虛拟機中首選的新生代收集器,在JDK1.6以及之前的版本中,除了Serial收集器外,隻有它能與CMS收集器配合工作。

在JDK 1.5時期,HotSpot推出了一款在強互動應用中幾乎可稱為有劃時代意義的垃圾收集器—CMS收集器(Concurrent Mark Sweep,本節稍後将詳細介紹這款收集器),這款收集器是HotSpot虛拟機中第一款真正意義上的并發(Concurrent)收集器,它第一次實作了讓垃圾收集線程與使用者線程(基本上)同時工作,用前面那個例子的話來說,就是做到了在你媽媽打掃房間的時候你還能同時往地上扔紙屑。

是以在JDK 1.5中使用CMS來收集老年代的時候,新生代隻能選擇ParNew或Serial收集器中的一個。ParNew收集器也是使用 -XX:+UseConcMarkSweepGC選項後的預設新生代收集器,也可以使用 -XX:+UseParNewGC選項來強制指定它。

3、固定JAVA虛拟機空間

源代碼:

package com.gc;

import java.util.ArrayList;

import java.util.List;

public class EasyParNew {

       public byte[] placeHolder =new byte[64 * 1024]; //占位符

       public static void main(String[]args) throws Exception{

              outOfMemoryByFixSize();

       }

       private static voidoutOfMemoryByFixSize() throws Exception{

              List<EasyParNew>list = new ArrayList<EasyParNew>();

              while(true){

                     EasyParNewserial = new EasyParNew();

                     list.add(serial);

                     Thread.sleep(10);//停頓10毫秒

              }

       }

       private static voidoutOfMemoryByExpansionSize() throws Exception{

              List<EasyParNew>list = new ArrayList<EasyParNew>();

              while(true){

                     EasyParNewserial = new EasyParNew();

                     list.add(serial);

                     Thread.sleep(10);//停頓10毫秒

              }

       }

}

參數:

-Xms30m -Xmx30m -Xmn10m -XX:+UseParNewGC -XX:+PrintGCDetails

其中,-XX:+UseParNewGC 表示要強制使用parNew收集器在新生代回收空間。

運作結果:

[GC [ParNew: 8137K->980K(9216K),0.0057214 secs] 8137K->8022K(29696K), 0.0057563 secs] [Times: user=0.01sys=0.00, real=0.01 secs]

[GC [ParNew: 9131K->1021K(9216K),0.0058799 secs] 16173K->16215K(29696K), 0.0059148 secs] [Times: user=0.00sys=0.00, real=0.01 secs]

[GC [ParNew: 9207K->9207K(9216K),0.0000123 secs][Tenured: 15194K->20447K(20480K), 0.0071258 secs]24401K->24289K(29696K), [Perm : 2086K->2086K(12288K)], 0.0071911 secs][Times: user=0.00 sys=0.00, real=0.01 secs]

[Full GC [Tenured:20447K->20447K(20480K), 0.0038141 secs] 29576K->29543K(29696K), [Perm :2086K->2086K(12288K)], 0.0038490 secs] [Times: user=0.00 sys=0.00, real=0.00secs]

[Full GC [Tenured:20447K->20436K(20480K), 0.0063466 secs] 29543K->29532K(29696K), [Perm :2086K->2084K(12288K)], 0.0063865 secs] [Times: user=0.00 sys=0.00, real=0.01secs]

Exception in thread "main"java.lang.OutOfMemoryError: Java heap space

    atcom.gc.EasyParNew.<init>(EasyParNew.java:12)

    atcom.gc.EasyParNew.outOfMemoryByFixSize(EasyParNew.java:25)

    atcom.gc.EasyParNew.main(EasyParNew.java:14)

Heap

 par new generation   total 9216K, used 9152K [0x03c10000,0x04610000, 0x04610000)

 eden space 8192K, 100% used [0x03c10000, 0x04410000, 0x04410000)

 from space 1024K,  93% used[0x04410000, 0x045000f0, 0x04510000)

 to   space 1024K,   0% used [0x04510000, 0x04510000, 0x04610000)

 tenured generation   total 20480K, used 20436K [0x04610000,0x05a10000, 0x05a10000)

   the space 20480K,  99% used [0x04610000, 0x05a052b8, 0x05a05400,0x05a10000)

4、可擴充的JAVA虛拟機空間

使用源代碼中對應的outOfMemoryByExpansionSize方法和配套的參數。

運作結果如下:

[GC [ParNew: 1990K->132K(2112K),0.0007742 secs] 24112K->24110K(30528K), 0.0007964 secs] [Times: user=0.00sys=0.00, real=0.00 secs]

[GC [ParNew: 1990K->130K(2112K),0.0008112 secs] 25969K->25965K(30528K), 0.0008477 secs] [Times: user=0.00sys=0.00, real=0.00 secs]

[GC [ParNew: 1988K->130K(2112K),0.0009319 secs] 27823K->27821K(30528K), 0.0009553 secs] [Times: user=0.00sys=0.00, real=0.00 secs]

[GC [ParNew: 1991K->134K(2112K),0.0008219 secs][Tenured: 29548K->29551K(29568K), 0.0031503 secs] 29682K->29679K(31680K),[Perm : 2086K->2086K(12288K)], 0.0040531 secs] [Times: user=0.01 sys=0.00,real=0.00 secs]

[GC [ParNew: 2562K->194K(2880K),0.0024877 secs] 32113K->32113K(40704K), 0.0025124 secs] [Times: user=0.00sys=0.00, real=0.00 secs]

[GC [ParNew: 2756K->196K(2880K),0.0010854 secs] 34676K->34680K(40704K), 0.0011096 secs] [Times: user=0.00sys=0.00, real=0.00 secs]

[GC [ParNew: 2758K->196K(2880K),0.0011059 secs] 37243K->37241K(40704K), 0.0011285 secs] [Times: user=0.03sys=0.00, real=0.00 secs]

[GC [ParNew: 2758K->2758K(2880K),0.0000123 secs][Tenured: 37045K->37813K(37824K), 0.0037394 secs]39803K->39798K(40704K), [Perm : 2086K->2086K(12288K)], 0.0037977 secs][Times: user=0.00 sys=0.00, real=0.00 secs]

[Full GC [Tenured:37813K->37813K(37824K), 0.0034278 secs] 40569K->40567K(40704K), [Perm :2086K->2086K(12288K)], 0.0034685 secs] [Times: user=0.00 sys=0.00, real=0.00secs]

[Full GC [Tenured:37813K->37792K(37824K), 0.0107084 secs] 40567K->40546K(40704K), [Perm :2086K->2084K(12288K)], 0.0107696 secs] [Times: user=0.01 sys=0.00, real=0.01secs]

Exception in thread "main"java.lang.OutOfMemoryError: Java heap space

    atcom.gc.EasyParNew.<init>(EasyParNew.java:12)

    atcom.gc.EasyParNew.outOfMemoryByExpansionSize(EasyParNew.java:39)

    atcom.gc.EasyParNew.main(EasyParNew.java:14)

Heap

 par new generation   total 2880K, used 2810K [0x03bf0000,0x03f00000, 0x03f00000)

 eden space 2624K,  99% used[0x03bf0000, 0x03e7e8e8, 0x03e80000)

 from space 256K,  75% used[0x03ec0000, 0x03ef0030, 0x03f00000)

 to   space 256K,   0% used [0x03e80000, 0x03e80000, 0x03ec0000)

 tenured generation   total 37824K, used 37792K [0x03f00000,0x063f0000, 0x063f0000)

  the space 37824K,  99% used[0x03f00000, 0x063e82e0, 0x063e8400, 0x063f0000)

 compacting perm gen  total 12288K, used 2105K [0x063f0000,0x06ff0000, 0x0a3f0000)

   the space 12288K,  17% used [0x063f0000, 0x065fe4a8, 0x065fe600,0x06ff0000)

5、ParNew收集器和Serial收集器的差異

ParNew收集器在單CPU的環境中絕對不會有比Serial收集器更好的效果,甚至由于存線上程互動的開銷,該收集器在通過超線程技術實作的兩個CPU的環境中都不能百分之百地保證能超越Serial收集器。當然,随着可以使用的CPU的數量的增加,它對于GC時系統資源的利用還是很有好處的。它預設開啟的收集線程數與CPU的數量相同,在CPU非常多(譬如32個,現在CPU動辄就4核加超線程,伺服器超過32個邏輯CPU的情況越來越多了)的環境下,可以使用-XX:ParallelGCThreads參數來限制垃圾收集的線程數。

并行(Parallel):指多條垃圾收集線程并行工作,但此時使用者線程仍然處于等待狀态。

并發(Concurrent):指使用者線程與垃圾收集線程同時執行(但不一定是并行的,可能會交替執行),使用者程式繼續運作,而垃圾收集程式運作于另一個CPU上。

[GC [DefNew:1986K->128K(2112K), 0.0011191 secs] 27809K->27808K(30528K), 0.0011425secs] [Times: user=0.00 sys=0.01, real=0.00 secs]

[GC [ParNew:1990K->132K(2112K), 0.0007742 secs] 24112K->24110K(30528K), 0.0007964secs] [Times: user=0.00 sys=0.00, real=0.00 secs]

其中的GC [ParNew 表示使用的是parNew收集器。

GC [DefNew 表示用的是serial收集器。

繼續閱讀