一.java堆内存溢出
首先设置堆内存大小 -Xms20M -Xmx20M 最大堆内存20M
java堆用于存储创建的对象,只要不断创建对象,且垃圾回收器无法触发GC,那么内存会不断增大,直到溢出
//内存溢出测试代码
static class OOMObject {
}
public static void main(String[] args) {
List list = new ArrayList<>();
while (true) {
list.add(new OOMObject());
}
}
运行结果:
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)
二.虚拟机栈 和 本地方法栈 内存溢出
由于在HotSpot虚拟机中不区分虚拟机栈和本地方法栈,因此,对于HotSpot而言,-Xoss设置本地方法栈的参数失效,栈的容量大小只有Xss参数设置。
虚拟机栈主要出现2种错误异常:
如果线程请求的栈深度大于虚拟机栈所允许的最大深度,抛出StackOverFlowError错误异常
如果虚拟机栈在拓展栈时无法申请到足够的内存空间,抛出OutOfMemoryError错误异常
设置-Xss128k
首先对第一种进行测试 栈帧长度不够
public class Fast {
private int stackLength=1;
public static void main(String[] args) {
Fast fast = new Fast();
try {
fast.stackLeak();
} catch (Throwable e) {
System.out.println("stack length:"+fast.stackLength);
throw e;
}
}
private void stackLeak() {
stackLength++;
stackLeak();
}
}
结果:抛出StackOverflowError
stack length:1623
Exception in thread "main" java.lang.StackOverflowError
at Fast.stackLeak(Fast.java:22)
第二种情况: 内存不够,局部变量过多
public class Fast {
private int stackLength=1;
public static void main(String[] args) {
Fast fast = new Fast();
try {
fast.stackLeak();
} catch (Throwable e) {
System.out.println("stack length:"+fast.stackLength);
throw e;
}
}
private void stackLeak() {
long unused1,unused2,unused3,unused4,unused5,unused6,unused7,unused8;
long unused11,unused12,unused13,unused14,unused15,unused16,unused17,unused18;
long unused21,unused22,unused23,unused24,unused25,unused26,unused27,unused28;
long unused31,unused32,unused33,unused34,unused35,unused36,unused37,unused38;
long unused41,unused42,unused43,unused44,unused45,unused46,unused47,unused48;
long unused51,unused52,unused53,unused54,unused55,unused56,unused57,unused58;
long unused61,unused62,unused63,unused64,unused65,unused66,unused67,unused68;
long unused81,unused82,unused83,unused84,unused85,unused86,unused87,unused88;
long unused71,unused72,unused73,unused74,unused75,unused76,unused77,unused78;
long unused91,unused92,unused93,unused94,unused95,unused96,unused97,unused98;
stackLength++;
stackLeak();
unused1 = unused2 = unused3 = unused4 =unused5 =unused6 =unused7 =unused8 =0;
unused11=unused12=unused13=unused14=unused15=unused16=unused17=unused18=0;
unused21=unused22=unused23=unused24=unused25=unused26=unused27=unused28=0;
unused31=unused32=unused33=unused34=unused35=unused36=unused37=unused38=0;
unused41=unused42=unused43=unused44=unused45=unused46=unused47=unused48=0;
unused51=unused52=unused53=unused54=unused55=unused56=unused57=unused58=0;
unused61=unused62=unused63=unused64=unused65=unused66=unused67=unused68=0;
unused71=unused72=unused73=unused74=unused75=unused76=unused77=unused78=0;
unused81=unused82=unused83=unused84=unused85=unused86=unused87=unused88=0;
unused91=unused92=unused93=unused94=unused95=unused96=unused97=unused98=0;
}
}
结果:
stack length:65
Exception in thread "main" java.lang.StackOverflowError
at Fast.stackLeak(Fast.java:31)
实验表明:不管哪种原因,都是抛出StackOverflowError
三.元数据区内存溢出
jdk1.8 设置 -XX:MaxMetaspaceSize=6m
public static void main(String[] args) {
while(true) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Test.class);
enhancer.setUseCache(false);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { return methodProxy.invokeSuper(obj, args);
}
});
enhancer.create();
}
}
结果:
Exception in thread "main"
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"
四.直接内存溢出
设置 -Xmx=20M -XX:MaxDirectMemorySize=10M
设置-XX:MaxDirectMemorySize 直接内存大小,没设置的 默认使用-Xmx设置
public class Fast {
private static final int _1MB = 1024*1024;
public static void main(String[] args) throws IllegalAccessException {
Field unsafeField = Unsafe.class.getDeclaredFields()[0];
unsafeField.setAccessible(true);
Unsafe unsafe = (Unsafe) unsafeField.get(null);
while (true) {
unsafe.allocateMemory(_1MB);
}
}
}
结果:
Exception in thread "main" java.lang.OutOfMemoryError
at sun.misc.Unsafe.allocateMemory(Native Method)
at Fast.main(Fast.java:17)