JVM1.8相關

1.JVM1.8在新版本的改進更新

  1.1對比:

    JDK 1.7 及以往的 JDK 版本中,Java 類信息、常量池、靜態變量都存儲在 Perm(永久代)裏。類的元數據和靜態變量在類加載的時候分配到 Perm,當類被卸載的時候垃圾收集器從 Perm 處理掉類的元數據和靜態變量。當然常量池的東西也會在 Perm 垃圾收集的時候進行處理。

    JDK 1.8 的對 JVM 架構的改造將類元數據放到本地內存中,另外,將常量池和靜態變量放到 Java 堆裏。HotSopt VM 將會爲類的元數據明確分配和釋放本地內存。在這種架構下,類元信息就突破了原來 -XX:MaxPermSize 的限制,現在可以使用更多的本地內存。這樣就從一定程度上解決了原來在運行時生成大量類的造成經常 Full GC 問題,如運行時使用反射、代理等。

  1.2注意:

    如果服務器內存足夠,升級到 JDK 1.8 修改 JVM 參數最簡單的辦法就是將 -XX:PermSize 和 -XX:MaxPermSize 參數替換爲 -XX:MetaspaceSize 和 -XX:MaxMetaspaceSize

    1.8中-XX:PermSize 和 -XX:MaxPermSize 已經失效,取而代之的是一個新的區域 —— Metaspace(元數據區)。

    使用JDK1.8以及之後的版本,不會再碰上“java.lang.OutOfMemoryError: PermGen space”這個錯誤了。

  1.3優勢理解:

    permSize:原來的jar包及你自己項目的class存放的內存空間,這部分空間是固定的,啓動參數裏面-permSize確定,如果你的jar包很多,經常會遇到permSize溢出,且每個項目都會佔用自己的permGen空間
    改成metaSpaces,各個項目會共享同樣的class內存空間,比如兩個項目都用了fast-json開源包,在mentaSpaces裏面只存一份class,提高內存利用率,且更利於垃圾回收

   1.4區別

    元空間並不在虛擬機中,而是使用本地內存。因此,默認情況下,元空間的大小僅受本地內存限制

   1.5參數來指定元空間的大小   

    -XX:MetaspaceSize,初始空間大小,達到該值就會觸發垃圾收集進行類型卸載,同時GC會對該值進行調整:如果釋放了大量的空間,就適當降低該值;如果釋放了很少的空間,那麼在不超過MaxMetaspaceSize時,適當提高該值。
    -XX:MaxMetaspaceSize,最大空間,默認是沒有限制的。

    除了上面兩個指定大小的選項以外,還有兩個與 GC 相關的屬性:
    -XX:MinMetaspaceFreeRatio,在GC之後,最小的Metaspace剩餘空間容量的百分比,減少爲分配空間所導致的垃圾收集
    -XX:MaxMetaspaceFreeRatio,在GC之後,最大的Metaspace剩餘空間容量的百分比,減少爲釋放空間所導致的垃圾收集

 

 

 

2.GC機制中,JVM對內存的分區

【需要說明:元數據區的大小限定於本地內存的大小,Eden、Survivor、Old區都是JVM管理下的堆中分配的內存區域】

 

JDK安裝目錄下 bin下面jvisualvm.exe  安裝Visual GC插件,選擇工具---插件

 

 

  2.1 GC是什麼

    GC(GarbageCollection)是垃圾回收機制,在java中開發人員無法使用指針來自由的管理內存,GC是JVM對內存(實際上就是對象)進行管理的方式。GC使得Java開發人員擺脫了繁瑣的內存管理工作,讓程序的開發更有效率。

  2.2 GC的工作原理

    自己的話:對於程序員來說,分配對象使用new關鍵字;釋放對象時,只要將對象所有引用賦值爲null,讓程序不能夠再訪問到這個對象,我們稱該對象爲"不可達的"或者"不被引用"。GC將負責回收所有"不可達"對象的內存空間。

    上圖中

      1》新new的對象都放在Eden區

      2》Eden區滿或者快滿的時候進行一次清理,不被引用的對象直接被幹掉;還有引用的對象,但是年齡比較大的,挪到S0區

      3》下次Eden區快滿的時候,會進行上一步的操作,並且將Eden和S0區的年紀大的對象放到S1區【原理上隨時保持S0和S1有一個是空的,用來存下一次的對象】

      4》下下次,Eden區快滿的時候,會進行上一步操作,並且將Eden和S1區的年紀大的對象放到S0區【此時S1區就是空的】

      5》直到Eden區快滿,S0或者S1也快滿的時候,這時候就把這兩個區的年紀大的對象放到Old區

      6》依次循環,直到Old區也快滿的時候,Eden區也快滿的時候,會對整個這一塊內存區域進行一次大清洗,騰出內存,爲之後的對象創建,程序運行騰地方。

  2.3 Minor GC、Major GC和Full GC

    清理Eden區和 Survivor區叫Minor GC

    清理Old區叫Major GC

    清理整個堆空間—包括年輕代和老年代叫Full GC

    參考:http://www.importnew.com/15820.html

 

jvm1.8 參數 在生產環境中  一般 

-XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m

 

 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC


-Xmx1024m (堆最大大小) 
-Xms1024m (堆默認大小) 
-Xmn256m (新生代大小) 
-Xss256k (棧最大深度大小) 
-XX:SurvivorRatio=8 (新生代分區比例 8:2) 
-XX:+UseConcMarkSweepGC (指定使用的垃圾收集器,這裏使用CMS收集器) 
-XX:+PrintGCDetails (打印詳細的GC日誌)

-XX:+UseG1GC (指定使用的垃圾收集器,這裏使用G1收集器,建議生產環境使用) 

-XX:MetaspaceSize=256m的含義到底是什麼呢?其實,這個JVM參數是指Metaspace擴容時觸發FullGC的初始化閾值,也是最小的閾值。這裏有幾個要點需要明確:

  1. 無論-XX:MetaspaceSize配置什麼值,Metaspace的初始容量一定是21807104(約20.8m);

  2. Metaspace由於使用不斷擴容到-XX:MetaspaceSize參數指定的量,就會發生FGC;且之後每次Metaspace擴容都會發生FGC;

  3. 如果Old區配置CMS垃圾回收,那麼第2點的FGC也會使用CMS算法進行回收;

  4. Meta區容量範圍爲[20.8m, MaxMetaspaceSize);

  5. 如果MaxMetaspaceSize設置太小,可能會導致頻繁FGC,甚至OOM;

任何一個JVM參數的默認值可以通過java -XX:+PrintFlagsFinal -version |grep JVMParamName獲取,例如:java -XX:+PrintFlagsFinal -version |grep MetaspaceSize

驗證

筆者的環境,服務啓動後,MU的值穩定在55296k,那麼設置-XX:MetaspaceSize=50m -XX:MaxMetaspaceSize=256m,按照上面的推理,會發生一次CMS GC,事實也確實如此,部分gc日誌如下所示:

 

[GC (CMS Initial Mark) [1 CMS-initial-mark: 13272K(524288K)] 253317K(996160K), 0.0566338 secs] [Times: user=0.13 sys=0.00, real=0.06 secs] 
[CMS-concurrent-mark-start]
[CMS-concurrent-mark: 0.033/0.033 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 
[CMS-concurrent-preclean-start]
[CMS-concurrent-preclean: 0.002/0.003 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-abortable-preclean-start]
 CMS: abort preclean due to time [CMS-concurrent-abortable-preclean: 2.904/5.006 secs] [Times: user=2.92 sys=0.00, real=5.01 secs] 
[GC (CMS Final Remark) [YG occupancy: 240743 K (471872 K)][Rescan (parallel) , 0.0676438 secs][weak refs processing, 0.0000916 secs][class unloading, 0.0085156 secs][scrub symbol table, 0.0139570 secs][scrub string table, 0.0007734 secs][1 CMS-remark: 13272K(524288K)] 254016K(996160K), 0.0922369 secs] [Times: user=0.24 sys=0.00, real=0.09 secs] 
[CMS-concurrent-sweep-start]
[CMS-concurrent-sweep: 0.006/0.006 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[CMS-concurrent-reset-start]
[CMS-concurrent-reset: 0.002/0.002 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 

 

通過 [GC (CMS Initial Mark) [1 CMS-initial-mark: 58004K(131072K)] 70447K(249088K), 0.0055264 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]這行日誌可知:Old區還遠遠達不到70%(-XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70)觸發CMS GC的條件。所以,這次CMS GC是Metaspace區擴容達到-XX:MetaspaceSize=50m觸發的。

建議

  1. MetaspaceSizeMaxMetaspaceSize設置一樣大;

  2. 具體設置多大,建議穩定運行一段時間後通過jstat -gc pid確認且這個值大一些,對於大部分項目256m即可。

驗證

當 MaxMetaspaceSize 設置太小,頻繁FGC,導致甚至OOM;

部分日誌如下:

[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10996K->10998K(524288K), 0.0597620 secs] 21650K->10998K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0598754 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10998K->10995K(524288K), 0.0369201 secs] 10998K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0370169 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10995K->10995K(524288K), 0.0364850 secs] 15189K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0365840 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10995K->10995K(524288K), 0.0373423 secs] 10995K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0374434 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10995K(524288K)] 15190K(996160K), 0.0006551 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.018/0.018 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10995K->10996K(524288K), 0.0575554 secs] 15190K->10996K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0576558 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10996K->10993K(524288K), 0.0388822 secs] 10996K->10993K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0389807 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10993K->10993K(524288K), 0.0367893 secs] 15187K->10993K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0368886 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10993K->10993K(524288K), 0.0369320 secs] 10993K->10993K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0370305 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10993K(524288K)] 15188K(996160K), 0.0005274 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.016/0.016 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10993K->10995K(524288K), 0.0568867 secs] 15188K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0570195 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10995K->10993K(524288K), 0.0409507 secs] 10995K->10993K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0410535 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10993K->11001K(524288K), 0.0435053 secs] 21647K->11001K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0436147 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 11001K->10995K(524288K), 0.0375241 secs] 11001K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0376231 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10995K(524288K)] 15190K(996160K), 0.0004919 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10995K->10995K(524288K), 0.0535647 secs] 21649K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0536974 secs] [Times: user=0.06 sys=0.00, real=0.05 secs] 
[Full GC (Last ditch collection) [CMS: 10995K->10994K(524288K), 0.0373442 secs] 10995K->10994K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0374459 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10994K->10995K(524288K), 0.0400787 secs] 15189K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0402923 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10995K->10995K(524288K), 0.0415953 secs] 10995K->10995K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0417313 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10995K(524288K)] 15189K(996160K), 0.0004332 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10995K->10997K(524288K), 0.0540166 secs] 15189K->10997K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0541175 secs] [Times: user=0.05 sys=0.00, real=0.05 secs] 
[Full GC (Last ditch collection) [CMS: 10997K->10997K(524288K), 0.0371319 secs] 10997K->10997K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0372279 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10997K->10997K(524288K), 0.0382360 secs] 15191K->10997K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0383428 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10997K->10997K(524288K), 0.0397804 secs] 10997K->10997K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0398800 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10997K->10996K(524288K), 0.0415289 secs] 15193K->10996K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0416360 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10996K->10996K(524288K), 0.0377221 secs] 10996K->10996K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0378574 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10996K(524288K)] 10996K(996160K), 0.0004777 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10996K->10996K(524288K), 0.0533219 secs] 10996K->10996K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0534206 secs] [Times: user=0.05 sys=0.00, real=0.05 secs] 
[Full GC (Last ditch collection) [CMS: 10996K->10996K(524288K), 0.0374463 secs] 10996K->10996K(996160K), [Metaspace: 29464K->29464K(1075200K)], 0.0375502 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10996K->11015K(524288K), 0.0421514 secs] 21650K->11015K(996160K), [Metaspace: 29472K->29472K(1075200K)], 0.0422935 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 11015K->11005K(524288K), 0.0417410 secs] 11015K->11005K(996160K), [Metaspace: 29472K->29472K(1075200K)], 0.0419002 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 11005K(524288K)] 15199K(996160K), 0.0005729 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.018 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 11005K->10999K(524288K), 0.0553269 secs] 15199K->10999K(996160K), [Metaspace: 29472K->29472K(1075200K)], 0.0554282 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10999K->10998K(524288K), 0.0392870 secs] 10999K->10998K(996160K), [Metaspace: 29472K->29472K(1075200K)], 0.0393887 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10998K->10998K(524288K), 0.0397466 secs] 15192K->10998K(996160K), [Metaspace: 29468K->29468K(1075200K)], 0.0401416 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10998K->10998K(524288K), 0.0433778 secs] 10998K->10998K(996160K), [Metaspace: 29468K->29468K(1075200K)], 0.0436206 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10998K(524288K)] 10998K(996160K), 0.0006964 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.018 secs] [Times: user=0.05 sys=0.02, real=0.02 secs] 
 (concurrent mode failure): 10998K->10997K(524288K), 0.0596584 secs] 10998K->10997K(996160K), [Metaspace: 29468K->29468K(1075200K)], 0.0597722 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10997K->10997K(524288K), 0.0367200 secs] 10997K->10997K(996160K), [Metaspace: 29468K->29468K(1075200K)], 0.0368175 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10997K->10999K(524288K), 0.0404981 secs] 21651K->10999K(996160K), [Metaspace: 29468K->29468K(1075200K)], 0.0405977 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10999K->10998K(524288K), 0.0382429 secs] 10999K->10998K(996160K), [Metaspace: 29468K->29468K(1075200K)], 0.0383780 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10998K(524288K)] 15192K(996160K), 0.0007988 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.022/0.022 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10998K->11007K(524288K), 0.0662522 secs] 15192K->11007K(996160K), [Metaspace: 29472K->29472K(1075200K)], 0.0663690 secs] [Times: user=0.06 sys=0.00, real=0.07 secs] 
[Full GC (Last ditch collection) [CMS: 11007K->10964K(524288K), 0.0407716 secs] 11007K->10964K(996160K), [Metaspace: 29472K->29472K(1075200K)], 0.0408863 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10964K->10962K(524288K), 0.0375811 secs] 15159K->10962K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0376826 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10962K->10962K(524288K), 0.0361406 secs] 10962K->10962K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0362370 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10962K(524288K)] 10962K(996160K), 0.0004835 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10962K->10962K(524288K), 0.0581142 secs] 10962K->10962K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0582166 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10962K->10962K(524288K), 0.0419855 secs] 10962K->10962K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0421280 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10962K->10962K(524288K), 0.0387063 secs] 10962K->10962K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0388109 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10962K->10962K(524288K), 0.0371432 secs] 10962K->10962K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0372394 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10962K(524288K)] 17421K(996160K), 0.0005023 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.016/0.016 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10962K->10969K(524288K), 0.0586031 secs] 17421K->10969K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0587152 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10969K->10968K(524288K), 0.0402176 secs] 10969K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0403834 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10968K->10964K(524288K), 0.0395990 secs] 10968K->10964K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0397337 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10964K->10964K(524288K), 0.0375395 secs] 10964K->10964K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0376404 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10964K(524288K)] 15158K(996160K), 0.0004721 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10964K->10963K(524288K), 0.0567881 secs] 15158K->10963K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0568911 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10963K->10963K(524288K), 0.0377466 secs] 10963K->10963K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0378478 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10963K->10963K(524288K), 0.0416776 secs] 10964K->10963K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0417782 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10963K->10963K(524288K), 0.0394801 secs] 10963K->10963K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0396151 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10963K(524288K)] 15159K(996160K), 0.0007194 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10963K->10966K(524288K), 0.0550673 secs] 21619K->10966K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0551686 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10966K->10966K(524288K), 0.0372895 secs] 10966K->10966K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0373936 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10966K->10966K(524288K), 0.0377889 secs] 15161K->10966K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0378945 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10966K->10966K(524288K), 0.0409924 secs] 10966K->10966K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0410907 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10966K(524288K)] 15161K(996160K), 0.0005863 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.018/0.019 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10966K->10968K(524288K), 0.0582391 secs] 15161K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0583396 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10968K->10968K(524288K), 0.0375418 secs] 10968K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0376384 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10968K->10968K(524288K), 0.0370213 secs] 15163K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0371185 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10968K->10968K(524288K), 0.0366392 secs] 10968K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0367340 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10968K(524288K)] 15163K(996160K), 0.0004850 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10968K->10968K(524288K), 0.0575500 secs] 15163K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0576588 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10968K->10968K(524288K), 0.0395850 secs] 10968K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0396838 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10968K->10986K(524288K), 0.0383948 secs] 21622K->10986K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0385036 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10986K->10977K(524288K), 0.0377620 secs] 10986K->10977K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0378644 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10977K(524288K)] 15172K(996160K), 0.0005240 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10977K->10973K(524288K), 0.0555831 secs] 15172K->10973K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0556830 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10973K->10973K(524288K), 0.0402066 secs] 10973K->10973K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0403364 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10973K->10974K(524288K), 0.0398905 secs] 15168K->10974K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0399919 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10974K->10974K(524288K), 0.0369311 secs] 10974K->10974K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0370515 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10974K(524288K)] 10974K(996160K), 0.0004963 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10974K->10973K(524288K), 0.0531260 secs] 15169K->10973K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0532296 secs] [Times: user=0.06 sys=0.00, real=0.05 secs] 
[Full GC (Last ditch collection) [CMS: 10973K->10973K(524288K), 0.0376419 secs] 10973K->10973K(996160K), [Metaspace: 29442K->29442K(1075200K)], 0.0377432 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10973K->10974K(524288K), 0.0406882 secs] 21627K->10974K(996160K), [Metaspace: 29445K->29445K(1075200K)], 0.0407966 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10974K->10974K(524288K), 0.0392890 secs] 10974K->10974K(996160K), [Metaspace: 29445K->29445K(1075200K)], 0.0393872 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10974K(524288K)] 10974K(996160K), 0.0004978 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.018/0.018 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10974K->10973K(524288K), 0.0580606 secs] 15169K->10973K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0581835 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10973K->10973K(524288K), 0.0393296 secs] 10973K->10973K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0394352 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10973K->10973K(524288K), 0.0377485 secs] 15167K->10973K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0378468 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10973K->10973K(524288K), 0.0426285 secs] 10973K->10973K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0427650 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10973K(524288K)] 10973K(996160K), 0.0006518 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.018/0.018 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10973K->10985K(524288K), 0.0552724 secs] 21627K->10985K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0553784 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10985K->10978K(524288K), 0.0373473 secs] 10985K->10978K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0374443 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10978K->10976K(524288K), 0.0364243 secs] 15173K->10976K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0365242 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10976K->10976K(524288K), 0.0381757 secs] 10976K->10976K(996160K), [Metaspace: 29446K->29446K(1075200K)], 0.0382755 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10976K(524288K)] 15171K(996160K), 0.0005042 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10976K->10976K(524288K), 0.0571292 secs] 15171K->10976K(996160K), [Metaspace: 29448K->29448K(1075200K)], 0.0572279 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10976K->10976K(524288K), 0.0410987 secs] 10976K->10976K(996160K), [Metaspace: 29448K->29448K(1075200K)], 0.0411986 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10976K->10976K(524288K), 0.0370404 secs] 21630K->10976K(996160K), [Metaspace: 29449K->29449K(1075200K)], 0.0371431 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10976K->10976K(524288K), 0.0362079 secs] 10976K->10976K(996160K), [Metaspace: 29449K->29449K(1075200K)], 0.0363024 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10976K(524288K)] 15171K(996160K), 0.0005074 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10976K->10975K(524288K), 0.0568865 secs] 15171K->10975K(996160K), [Metaspace: 29449K->29449K(1075200K)], 0.0569955 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10975K->10975K(524288K), 0.0433910 secs] 10975K->10975K(996160K), [Metaspace: 29449K->29449K(1075200K)], 0.0435500 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10975K->10976K(524288K), 0.0405072 secs] 15169K->10976K(996160K), [Metaspace: 29449K->29449K(1075200K)], 0.0406177 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10976K->10976K(524288K), 0.0372435 secs] 10976K->10976K(996160K), [Metaspace: 29449K->29449K(1075200K)], 0.0373406 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10976K(524288K)] 21630K(996160K), 0.0006310 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10976K->11016K(524288K), 0.0575538 secs] 21630K->11016K(996160K), [Metaspace: 29453K->29453K(1075200K)], 0.0576634 secs] [Times: user=0.06 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 11016K->10995K(524288K), 0.0368434 secs] 11016K->10995K(996160K), [Metaspace: 29453K->29453K(1075200K)], 0.0369395 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10995K->10979K(524288K), 0.0402371 secs] 15190K->10979K(996160K), [Metaspace: 29453K->29453K(1075200K)], 0.0403805 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10979K->10973K(524288K), 0.0396410 secs] 10979K->10973K(996160K), [Metaspace: 29453K->29453K(1075200K)], 0.0397870 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10973K(524288K)] 10973K(996160K), 0.0005205 secs] [Times: user=0.06 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10973K->10973K(524288K), 0.0550588 secs] 10973K->10973K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0551630 secs] [Times: user=0.05 sys=0.00, real=0.05 secs] 
[Full GC (Last ditch collection) [CMS: 10973K->10973K(524288K), 0.0369433 secs] 10973K->10973K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0370398 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
[Full GC (Metadata GC Threshold) [CMS: 10973K->10966K(524288K), 0.0408875 secs] 10973K->10966K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0409917 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[Full GC (Last ditch collection) [CMS: 10966K->10966K(524288K), 0.0408376 secs] 10966K->10966K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0410226 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 10966K(524288K)] 10966K(996160K), 0.0009718 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
[Full GC (Metadata GC Threshold) [CMS[CMS-concurrent-mark: 0.017/0.017 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
 (concurrent mode failure): 10966K->10968K(524288K), 0.0570590 secs] 15161K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0571933 secs] [Times: user=0.05 sys=0.00, real=0.06 secs] 
[Full GC (Last ditch collection) [CMS: 10968K->10968K(524288K), 0.0377433 secs] 10968K->10968K(996160K), [Metaspace: 29438K->29438K(1075200K)], 0.0378456 secs] [Times: user=0.05 sys=0.00, real=0.04 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Metaspace
[Full GC (Metadata GC Threshold) [CMS: 10968K->7605K(524288K), 0.0339590 secs] 46790K->7605K(996160K), [Metaspace: 29443K->29443K(1075200K)], 0.0340657 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 
[Full GC (Last ditch collection) [CMS: 7605K->7604K(524288K), 0.0299186 secs] 7605K->7604K(996160K), [Metaspace: 29443K->29443K(1075200K)], 0.0300518 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 
[GC (CMS Initial Mark) [1 CMS-initial-mark: 7604K(524288K)] 7604K(996160K), 0.0005979 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[CMS-concurrent-mark-start]
Heap
 par new generation   total 471872K, used 8390K [0x0000000080000000, 0x00000000a0000000, 0x00000000a0000000)
  eden space 419456K,   2% used [0x0000000080000000, 0x00000000808319d0, 0x00000000999a0000)
  from space 52416K,   0% used [0x000000009ccd0000, 0x000000009ccd0000, 0x00000000a0000000)
  to   space 52416K,   0% used [0x00000000999a0000, 0x00000000999a0000, 0x000000009ccd0000)
 concurrent mark-sweep generation total 524288K, used 7604K [0x00000000a0000000, 0x00000000c0000000, 0x0000000100000000)
 Metaspace       used 29446K, capacity 30324K, committed 30720K, reserved 1075200K
  class space    used 4003K, capacity 4224K, committed 4352K, reserved 1048576K

 

驗證string常量存在堆內存中,demo如下

  /**
     * 測試
     *
     * @param args
     */
    public static void main(String[] args) throws Exception {
        try {
            List<String> list = new ArrayList<String>();
            for (int i = 0; ; i++) {
                System.out.println(i);
                list.add(""+i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }



    }

 

JVM參數如下

-XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m -Xms50m -Xmx50m -Xmn20m -Xss256k -XX:SurvivorRatio=8 -XX:+UseG1GC  -XX:+PrintGCDetails 

運行結果如下

[GC pause (G1 Humongous Allocation) (young), 0.0004699 secs]
   [Parallel Time: 0.2 ms, GC Workers: 4]
      [GC Worker Start (ms): Min: 14630.5, Avg: 14630.5, Max: 14630.6, Diff: 0.0]
      [Ext Root Scanning (ms): Min: 0.1, Avg: 0.1, Max: 0.1, Diff: 0.0, Sum: 0.5]
      [Update RS (ms): Min: 0.0, Avg: 0.0, Max: 0.1, Diff: 0.0, Sum: 0.2]
         [Processed Buffers: Min: 1, Avg: 1.5, Max: 2, Diff: 1, Sum: 6]
      [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Object Copy (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
         [Termination Attempts: Min: 1, Avg: 1.0, Max: 1, Diff: 0, Sum: 4]
      [GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [GC Worker Total (ms): Min: 0.2, Avg: 0.2, Max: 0.2, Diff: 0.0, Sum: 0.8]
      [GC Worker End (ms): Min: 14630.7, Avg: 14630.7, Max: 14630.7, Diff: 0.0]
   [Code Root Fixup: 0.0 ms]
   [Code Root Purge: 0.0 ms]
   [Clear CT: 0.1 ms]
   [Other: 0.2 ms]
      [Choose CSet: 0.0 ms]
      [Ref Proc: 0.1 ms]
      [Ref Enq: 0.0 ms]
      [Redirty Cards: 0.1 ms]
      [Humongous Register: 0.0 ms]
      [Humongous Reclaim: 0.0 ms]
      [Free CSet: 0.0 ms]
   [Eden: 0.0B(20.0M)->0.0B(20.0M) Survivors: 0.0B->0.0B Heap: 48.2M(50.0M)->48.2M(50.0M)]
 [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure)  48M->47M(50M), 0.1027376 secs]
   [Eden: 0.0B(20.0M)->0.0B(20.0M) Survivors: 0.0B->0.0B Heap: 48.2M(50.0M)->47.5M(50.0M)], [Metaspace: 3914K->3914K(1056768K)]
 [Times: user=0.17 sys=0.00, real=0.10 secs] 
[Full GC (Allocation Failure)  47M->47M(50M), 0.0880405 secs]
   [Eden: 0.0B(20.0M)->0.0B(20.0M) Survivors: 0.0B->0.0B Heap: 47.5M(50.0M)->47.5M(50.0M)], [Metaspace: 3914K->3914K(1056768K)]
 [Times: user=0.14 sys=0.00, real=0.09 secs] 
[GC concurrent-mark-abort]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3210)
	at java.util.Arrays.copyOf(Arrays.java:3181)
	at java.util.ArrayList.grow(ArrayList.java:265)
	at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239)
	at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:231)
	at java.util.ArrayList.add(ArrayList.java:462)
	at com.zsj.tool.util.DateUtil.main(DateUtil.java:524)
Heap
 garbage-first heap   total 51200K, used 48676K [0x00000000fce00000, 0x00000000fcf00190, 0x0000000100000000)
  region size 1024K, 1 young (1024K), 0 survivors (0K)
 Metaspace       used 3944K, capacity 4600K, committed 4864K, reserved 1056768K
  class space    used 419K, capacity 428K, committed 512K, reserved 1048576K

Process finished with exit code 1

可以看到   

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space,堆內存溢出。

 

元數據區
元數據區OOM測試:

//藉助cglib框架生成新類。

demo

package com.zsj.tool.util;

import org.springframework.cglib.proxy.CallbackFilter;
import org.springframework.cglib.proxy.Dispatcher;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;

import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;

/**
 * @Description TODO
 * @Date 2019/4/19 9:25
 * @Author zsj
 */
public class MetaSpaceOomMock {
   //藉助cglib框架生成新類。
    public static void main(String[] args) {
        ClassLoadingMXBean loadingBean = ManagementFactory.getClassLoadingMXBean();
        while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(MetaSpaceOomMock.class);
            enhancer.setCallbackTypes(new Class[]{Dispatcher.class, MethodInterceptor.class});
            enhancer.setCallbackFilter(new CallbackFilter() {
                @Override
                public int accept(Method method) {
                    return 1;
                }

                @Override
                public boolean equals(Object obj) {
                    return super.equals(obj);
                }
            });

            Class clazz = enhancer.createClass();
            System.out.println(clazz.getName());
            //顯示數量信息(共加載過的類型數目,當前還有效的類型數目,已經被卸載的類型數目)
            System.out.println("total: " + loadingBean.getTotalLoadedClassCount());
            System.out.println("active: " + loadingBean.getLoadedClassCount());
            System.out.println("unloaded: " + loadingBean.getUnloadedClassCount());
        }
    }

}

jvm參數

-XX:MetaspaceSize=200m -XX:MaxMetaspaceSize=200m -Xms50m -Xmx50m -Xmn20m -Xss256k -XX:SurvivorRatio=8 -XX:+UseG1GC  -XX:+PrintGCDetails 

運行結果

Exception in thread "main" java.lang.IllegalStateException: Unable to load cache item
	at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:79)
	at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116)
	at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
	at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480)
	at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:337)
	at com.zsj.tool.util.MetaSpaceOomMock.main(MetaSpaceOomMock.java:37)
Caused by: java.lang.OutOfMemoryError: Metaspace
	at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:345)
	at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:492)
	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
	at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
	at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
	... 6 more
Heap
 garbage-first heap   total 51200K, used 49464K [0x00000000fce00000, 0x00000000fcf00190, 0x0000000100000000)
  region size 1024K, 2 young (2048K), 0 survivors (0K)
 Metaspace       used 204322K, capacity 204600K, committed 204800K, reserved 1239040K
  class space    used 16052K, capacity 16141K, committed 16256K, reserved 1048576K

Process finished with exit code 1

 

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