一般有這幾種異常:
java.long.StackOverflowError
與棧(stack)相關,因爲每個方法都會有一個棧幀,遞歸調用時容易出現這種異常。
public static void main(String[] args) {
int n=10;
add(n);
}
public static void add(int n){
add(n-1);
}
//Exception in thread "main" java.lang.StackOverflowError
在調用方法時要注意確保方法調用的結束,不能無限循環調用
- java.long.OutOfMemoryError
比較常見的java.lang.OutOfMemoryError:heap space
,java.lang.OutOfMemoryError: PermGen space
1, java.lang.OutOfMemoryError: PermGen space
是說 JVM 裏的方法區的異常溢出,由於JVM在默認的情況下,Perm默認爲64M,而很多程序需要大量的Perm區內存,導致OutOfMemoryError: PermGen space異常。
解決方法,增大JVM的 -XX:MaxPermSize 啓動參數
2, java.lang.OutOfMemoryError:heap space 其它 OutOfMemoryError
而這個異常是因爲 JVM堆內存或者說總體分少了。
解決方法,更改 -Xms -Xmx 啓動參數
public class Errors {
public static void main(String[] args) {
Vector<BigObject> vector = new Vector<BigObject>();
while(true){
vector.add(new BigObject());
}
}
}
class BigObject{
int n=10;
public void test(){
n=20;
}
}
//Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
參考:http://stackoverflow.com/questions/11435613/whats-the-difference-between-stackoverflowerror-and-outofmemoryerror;
http://blog.csdn.net/chaofanwei/article/details/19483101;
http://blog.csdn.net/roland101/article/details/2197444