【參考】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.