1.Java 堆溢出:
堆:Java中的堆是用来存储对象本身的以及数组(数组引用是存放在Java栈中的)。堆是被所有线程共享的,在JVM中只有一个堆。
public class jvm {
public static void main(String[] args) {
List<TestCase> cases = new ArrayList<TestCase>();
while(true){
cases.add(new TestCase());
}
}
}
2.栈内存溢出
public class StackOverFlow {
private int a;
public void plus(){
a++;
plus();
}
public static void main(String[] args) {
StackOverFlow stackOverFlow = new StackOverFlow();
try {
stackOverFlow.plus();
} catch (Exception e) {
System.out.println("Exception:stack length:"+stackOverFlow.a);
e.printStackTrace();
} catch (Error e){
System.out.println("Exception:stack length:"+stackOverFlow.a);
e.printStackTrace();
}
}
}
3.常量池内存溢出
public class ConstantOutOfMemory {
public static void main(String[] args) {
try {
List<String> strings = new ArrayList<String>();
int i = 0;
while(true){
strings.add(String.valueOf(i++).intern());
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}