觸發(fā)Full gc條件

【參考】https://blog.csdn.net/scugxl/article/details/50935863

1.調(diào)用System.gc

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * created by: gaoxingliang@outlook.com
 * created:2016年3月20日
 */

/**
 * -XX:+UseSerialGC -Xms200M -Xmx200M -Xmn32m -XX:SurvivorRatio=8 -XX:+PrintGCDetails
 * @author gxl
 *
 */
public class SimulateFullGc
{
    public static void main(String[] args)
    {
        //模擬fullgc場景
        //場景1 使用System.gc
        List<Object> l = new ArrayList<Object>();
        for (int i =0; i< 100;i++)
        {
            l.add(new byte[1024*1024 ]);
            if (i % 10 ==0)
            {
                System.gc();
            }
        }

    }
}

> [**Full GC (System)** [Tenured: 0K->1495K(172032K), 0.0048354 secs] 2073K->1495K(201536K), [Perm : 2529K->2529K(21248K)], 0.0048900 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
> [Full GC (System) [Tenured: 1495K->11735K(172032K), 0.0064495 secs] 13310K->11735K(201536K), [Perm : 2532K->2532K(21248K)], 0.0064752 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]

由System.gc調(diào)用的gc標(biāo)記Full GC (System).

2.老年代空間不足

/**
 * 
 * created by: gaoxingliang@outlook.com
 * created:2016年3月20日
 */

/**
 * -XX:+UseSerialGC -Xms200M -Xmx200M -Xmn32m -XX:SurvivorRatio=8 -XX:+PrintGCDetails
 * @author gxl
 *
 */
public class SimulateFullGc
{
    public static void main(String[] args)
    {
        //模擬fullgc場景
        //老年代空間不足
        //按照上面的參數(shù)推算:老年代大小: 200 -32m = 168M

        byte [] MAXOBJ = new byte [1024 * 1024 * 100]; // 100M

        byte [] MAXOBJ2 = new byte [1024 * 1024 * 70]; // 60M
        MAXOBJ = null;

        byte [] MAXOBJ3 = new byte [1024 * 1024 * 100]; // 60M
    }
}

> [GC [DefNew: 5145K->470K(29504K), 0.0029970 secs][Tenured: 106496K->106966K(172032K), 0.0027630 secs] 107545K->106966K(201536K), [Perm : 2528K->2528K(21248K)], 0.0057990 secs] [Times: user=0.00 sys=0.02, real=0.01 secs] 
> [Full GC [Tenured: 106966K->106952K(172032K), 0.0024331 secs] 106966K->106952K(201536K), [Perm : 2528K->2527K(21248K)], 0.0024488 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
> Exception in thread “main” java.lang.OutOfMemoryError: Java heap space 
> at SimulateFullGc.main(SimulateFullGc.java:27)

[GC [DefNew: 5145K->470K(29504K), 0.0029970 secs][Tenured: 106496K->106966K(172032K), 0.0027630 secs] 107545K->106966K(201536K), [Perm : 2528K->2528K(21248K)], 0.0057990 secs] [Times: user=0.00 sys=0.02, real=0.01 secs] 
[Full GC [Tenured: 106966K->106952K(172032K), 0.0024331 secs] 106966K->106952K(201536K), [Perm : 2528K->2527K(21248K)], 0.0024488 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Exception in thread “main” java.lang.OutOfMemoryError: Java heap space 
at SimulateFullGc.main(SimulateFullGc.java:27)

3.永久代空間不足

方式 在jdk6 上:

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * created by: gaoxingliang@outlook.com
 * created:2016年3月20日
 */

/**
 * -XX:+UseSerialGC -Xms200M -Xmx200M -Xmn32m -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:MaxPermSize=10M
 * @author gxl
 *
 */
public class SimulateFullGc
{
    public static void main(String[] args)
    {
        //模擬fullgc場景
        //持久代空間不足
        List<String> list = new ArrayList<String>();
        int i = 0;
        while (true)
        {
            list.add(String.valueOf("ABCD:"  + i ++).intern());
        }
    }
}

> [GC [DefNew: 26240K->937K(29504K), 0.0040883 secs] 26240K->937K(201536K), 0.0041121 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
> [Full GC [Tenured: 0K->1325K(172032K), 0.0362063 secs] 17898K->1325K(201536K), [Perm : 20479K->20479K(20480K)], 0.0362549 secs] [Times: user=0.03 sys=0.00, real=0.04 secs] 
> [Full GC [Tenured: 1325K->1325K(172032K), 0.0326822 secs] 1325K->1325K(201536K), [Perm : 20479K->20479K(20480K)], 0.0327085 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 
> [Full GC [Tenured: 1325K->1325K(172032K), 0.0128924 secs] 1821K->1325K(201536K), [Perm : 20479K->3734K(20480K)], 0.0129210 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
> Exception in thread “main” java.lang.OutOfMemoryError: PermGen space 
> at java.lang.String.intern(Native Method) 
> at SimulateFullGc.main(SimulateFullGc.java:25)

String.intern 會拷貝實例到永久代中.

在jdk1.7 不會,所以可以加載class來模擬:

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * 
 * created by: gaoxingliang@outlook.com
 * created:2016年3月20日
 */

/**
 * -XX:+UseSerialGC -Xms200M -Xmx200M -Xmn32m -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:MaxPermSize=10M
 * @author gxl
 *
 */
public class SimulateFullGc
{
    public static void main(String[] args)
    {
        //模擬fullgc場景
        //持久代空間不足
        //class 加載信息
        //需要cglib + asm (http://forge.ow2.org/projects/asm/)
        while (true)
        {
            Enhancer en = new Enhancer();
            en.setSuperclass(OOMObject.class);
            en.setUseCache(false);
            en.setCallback(new MethodInterceptor()
            {

                @Override
                public Object intercept(Object arg0, Method arg1, Object[] arg2,
                        MethodProxy arg3) throws Throwable
                {
                    // TODO Auto-generated method stub
                    return null;
                }
            });
            en.create();
        }
    }
    static class OOMObject
    {

    }
}

> [GC [DefNew: 26240K->1611K(29504K), 0.0039283 secs] 26240K->1611K(201536K), 0.0039524 secs] [Times: user=0.03 sys=0.00, real=0.00 secs] 
>
[GC [DefNew: 28043K->1735K(29504K), 0.0039443 secs] 33739K->7817K(201536K), 0.0039660 secs] [Times: user=0.03 sys=0.00, real=0.00 secs] 
[Full GC [Tenured: 6082K->7989K(172032K), 0.0322856 secs] 23097K->7989K(201536K), [Perm : 20479K->20479K(20480K)], 0.0323121 secs] [Times: user=0.05 sys=0.00, real=0.03 secs] 
[Full GC [Tenured: 7989K->7989K(172032K), 0.0233015 secs] 7989K->7989K(201536K), [Perm : 20479K->20479K(20480K)], 0.0233266 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
[Full GC [Tenured: 7989K->7989K(172032K), 0.0199921 secs] 8515K->7989K(201536K), [Perm : 20479K->20479K(20480K)], 0.0200187 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 
[Full GC [Tenured: 7989K->3354K(172032K), 0.0250219 secs] 7989K->3354K(201536K), [Perm : 20479K->20477K(20480K)], 0.0250530 secs] [Times: user=0.02 sys=0.00, real=0.03 secs] 
**Exception in thread “main” [Full GC [Tenured: 3354K->3355K(172032K), 0.0198650 secs] 3880K->3355K(201536K), [Perm : 20479K->20479K(20480K)], 0.0198919 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]** 
[Full GC [Tenured: 3355K->3355K(172032K), 0.0198493 secs] 3355K->3355K(201536K), [Perm : 20479K->20479K(20480K)], 0.0198762 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 
[Full GC [Tenured: 3355K->3355K(172032K), 0.0197512 secs] 3880K->3355K(201536K), [Perm : 20479K->20479K(20480K)], 0.0197814 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
[Full GC [Tenured: 3355K->3285K(172032K), 0.0245018 secs] 3355K->3285K(201536K), [Perm : 20479K->20478K(20480K)], 0.0245283 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]

cglib & asm jar download:[http://download.csdn.net/detail/u010926243/6721023](http://download.csdn.net/detail/u010926243/6721023)
···

4. gc 擔(dān)保失敗

空間分配擔(dān)保失敗.
在發(fā)生MinorGC前,檢查老年代是否有連續(xù)空間,如果有,則執(zhí)行,如果沒有,根據(jù)設(shè)置:-XX:-HandlePromotionFailure 指定,如果打開,那么繼續(xù)檢查,當(dāng)前老年代最大可用連續(xù)空間大于平均歷次晉升到老年代大小,如果大于,則進行MinorGC,否則進行FullGC,如果HandlePromotionFailure 不設(shè)置 直接進行FullGC.
大致就是這樣:


flow

代碼:
該參數(shù):-XX:-HandlePromotionFailure 在JDK 6U24中移除.后續(xù)判斷只要剩余連續(xù)大于當(dāng)前新生代或者歷次晉升平均大小就會執(zhí)行minorgc.

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option HandlePromotionFailure; support was removed in 6.0_24

搞了好久終于搞出來了:

import java.lang.reflect.Field;

import sun.misc.Unsafe;

/**
 * 
 * created by: gaoxingliang@outlook.com
 * created:2016年3月20日
 */

/**
 * -Xms20M -Xmx20M -Xmn10m -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:-HandlePromotionFailure -XX:MaxTenuringThreshold=1
 * @author gxl
 *
 */
public class SimulateFullGc
{
    private static final int MB = 1024 * 1024;

    public static void main(String[] args) throws Exception
    {
        // 模擬fullgc場景
        // 提升擔(dān)保
        // 提升擔(dān)保
        byte[] M6, M3, M4, M5, M7, M8;
        M6 = new byte[6 * MB];
        M6[0] = 0;
        M6[0] = 0;
        // 使用2次保證下次需要的時候可以晉升到老年代 會晉升那么 晉升經(jīng)驗值為6M
        M3 = new byte[4 * MB];
        M4 = new byte[2 * MB];
        M4 = null;
        M5 = new byte[2 * MB];
        M5[0] = 0;
        M5[0] = 0;
        M7 = new byte[2 * MB];
        M7[0] = 0;
        M7[0] = 0;
        M7 = null;
        M8 = new byte[3 * MB];
        // 最終如下對象 老年代 M6 + M8 = 9M
        // 年輕代:M3 + M5 = 6M = 6144K
        System.out.println("M6 HEX:0x" + Long.toHexString(addressOf(M6)));
        System.out.println("M5 HEX:0x" + Long.toHexString(addressOf(M5)));
        System.out.println("M3 HEX:0x" + Long.toHexString(addressOf(M3)));
        System.out.println("M8 HEX:0x" + Long.toHexString(addressOf(M8)));

    }

    private static Unsafe unsafe;

    static
    {
        try
        {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            unsafe = (Unsafe) field.get(null);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static long addressOf(Object o) throws Exception
    {
        Object[] array = new Object[] { o };

        long baseOffset = unsafe.arrayBaseOffset(Object[].class);
        int addressSize = unsafe.addressSize();
        long objectAddress;
        switch (addressSize)
        {
        case 4:
            objectAddress = unsafe.getInt(array, baseOffset);
            break;
        case 8:
            objectAddress = unsafe.getLong(array, baseOffset);
            break;
        default:
            throw new Error("unsupported address size: " + addressSize);
        }

        return (objectAddress);
    }

}

> [GC [DefNew: 6487K->149K(9216K), 0.0027691 secs] 6487K->6293K(19456K), 0.0027839 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
> [GC [DefNew: 6379K->6379K(9216K), 0.0000060 secs][Tenured: 6144K->6144K(10240K), 0.0048112 secs] 12523K->10389K(19456K), [Perm : 374K->374K(12288K)], 0.0048426 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
> [Full GC [Tenured: 8192K->6144K(10240K), 0.0032886 secs] 14485K->12437K(19456K), [Perm : 374K->374K(12288K)], 0.0033058 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
> M6 HEX:0x33990000 
> M5 HEX:0x333b5520 
> M3 HEX:0x32f90000 
> M8 HEX:0x33f90010 
> Heap 
> def new generation total 9216K, used 6514K [0x32f90000, 0x33990000, 0x33990000) 
> eden space 8192K, 79% used [0x32f90000, 0x335ec808, 0x33790000) 
> from space 1024K, 0% used [0x33890000, 0x33890000, 0x33990000) 
> to space 1024K, 0% used [0x33790000, 0x33790000, 0x33890000) 
> tenured generation total 10240K, used 9216K [0x33990000, 0x34390000, 0x34390000) 
> the space 10240K, 90% used [0x33990000, 0x34290020, 0x34290200, 0x34390000) 
> compacting perm gen total 12288K, used 374K [0x34390000, 0x34f90000, 0x38390000) 
> the space 12288K, 3% used [0x34390000, 0x343ed960, 0x343eda00, 0x34f90000) 
> ro space 10240K, 54% used [0x38390000, 0x3890c510, 0x3890c600, 0x38d90000) 
> rw space 12288K, 55% used [0x38d90000, 0x3942fb78, 0x3942fc00, 0x39990000)

注意對象位置.

5. Cocurrent mode failure

發(fā)生在cms的清理sweep階段,發(fā)現(xiàn)有新的垃圾產(chǎn)生,而且老年代沒有足夠空間導(dǎo)致的.
關(guān)于cms:
初始標(biāo)記(STW) - >并發(fā)標(biāo)記 ->重新標(biāo)記(STW) ->并發(fā)清除.
STW = stop the world.
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末卿啡,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子谬运,更是在濱河造成了極大的恐慌勿璃,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蔬充,死亡現(xiàn)場離奇詭異牧抵,居然都是意外死亡笛匙,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門犀变,熙熙樓的掌柜王于貴愁眉苦臉地迎上來膳算,“玉大人,你說我怎么就攤上這事弛作√榉洌” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵映琳,是天一觀的道長机隙。 經(jīng)常有香客問我,道長萨西,這世上最難降的妖魔是什么有鹿? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮谎脯,結(jié)果婚禮上葱跋,老公的妹妹穿的比我還像新娘。我一直安慰自己源梭,他們只是感情好娱俺,可當(dāng)我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著废麻,像睡著了一般荠卷。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上烛愧,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天油宜,我揣著相機與錄音掂碱,去河邊找鬼。 笑死慎冤,一個胖子當(dāng)著我的面吹牛疼燥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蚁堤,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼悴了,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了违寿?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤熟空,失蹤者是張志新(化名)和其女友劉穎藤巢,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體息罗,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡掂咒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了迈喉。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片绍刮。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖挨摸,靈堂內(nèi)的尸體忽然破棺而出孩革,到底是詐尸還是另有隱情,我是刑警寧澤得运,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布膝蜈,位于F島的核電站,受9級特大地震影響熔掺,放射性物質(zhì)發(fā)生泄漏饱搏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一置逻、第九天 我趴在偏房一處隱蔽的房頂上張望推沸。 院中可真熱鬧,春花似錦券坞、人聲如沸鬓催。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽深浮。三九已至,卻和暖如春眠冈,著一層夾襖步出監(jiān)牢的瞬間飞苇,已是汗流浹背菌瘫。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留布卡,地道東北人雨让。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像忿等,于是被迫代替她去往敵國和親栖忠。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容