1、堆内存溢出示例:
/**
* Created by Chengrui on 2015/8/23.
* java OutOfMemoryError test
*/
public class Heap {
public static void main(String[] args){
List list = new ArrayList();
while (true){
list.add(new Heap());
}
}
}
console:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
2、栈内存溢出示例:
/**
* Created by Chengrui on 2015/8/23.
* java StackOverflowError test
*/
public class Stack {
public void dgMethod(){
dgMethod();
}
public static void main(String[] args){
new Stack().dgMethod();
}
}
console:
Exception in thread "main" java.lang.StackOverflowError