java常見記憶體溢位情形
阿新 • • 發佈:2018-12-31
虛擬機器棧溢位(如果虛擬機器在擴充套件時無法申請到足夠的記憶體空間將丟擲OutOfMemoryError)
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid2076.hprof ...
Exception in thread "main" Heap dump file created [24777740 bytes in 1.181 secs]
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at com.jvm.memory.HeapOOM.main(HeapOOM.java:14)
java堆用於儲存物件例項,我們只要不斷的建立物件,達到最大容量限制後,最後就會導致堆記憶體溢位,
我們如果在系統中出現這種情況就要利用映像分析工具以及對dump轉儲出來的快照進行分析,看建立這些類是否是有
Exception in thread "main" java.lang.StackOverflowError
at com.jvm.memory.JavaJVMStackOOM.stackLeak(JavaJVMStackOOM.java:16)
at com.jvm.memory.JavaJVMStackOOM.stackLeak(JavaJVMStackOOM.java:17)
Error occurred during initialization of VM
Too small initial permanent heap
package com.jvm.memory; import java.util.ArrayList; import java.util.List; public class HeapOOM { /** * VM 執行時引數 -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError * @param args */ public static void main(String[] args) { List<OOMObject> list = new ArrayList<OOMObject>(); while(true){ list.add(new OOMObject()); } } static class OOMObject{ } }
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid2076.hprof ...
Exception in thread "main" Heap dump file created [24777740 bytes in 1.181 secs]
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at com.jvm.memory.HeapOOM.main(HeapOOM.java:14)
java堆用於儲存物件例項,我們只要不斷的建立物件,達到最大容量限制後,最後就會導致堆記憶體溢位,
我們如果在系統中出現這種情況就要利用映像分析工具以及對dump轉儲出來的快照進行分析,看建立這些類是否是有
必須要的。從2個方面檢查1:看看對引數是不是設定有問題,是否還可以調大。2:檢查是不是某些物件的生命週期過長。
本地方法棧溢位(如果執行緒請求的棧深度大於虛擬機器本身所允許的最大深度,將丟擲StackOverflowError)
package com.jvm.memory; public class JavaJVMStackOOM { /** * VM args -Xss128k * @param args */ public static void main(String[] args) { JavaJVMStackOOM oom = new JavaJVMStackOOM(); oom.stackLeak(); } private int stackLenth=1; public void stackLeak(){ stackLenth++; stackLeak(); } }
Exception in thread "main" java.lang.StackOverflowError
at com.jvm.memory.JavaJVMStackOOM.stackLeak(JavaJVMStackOOM.java:16)
at com.jvm.memory.JavaJVMStackOOM.stackLeak(JavaJVMStackOOM.java:17)
常量池溢位
package com.jvm.memory;
import java.util.ArrayList;
import java.util.List;
public class RuntimeConstantPoolOOM {
/**
* VM Args -XX:PermSize=10 -XX:MaxPermSize=10
* @param args
*/
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
int i=0;
while(true){
list.add(String.valueOf(i++).intern());
}
}
}
Error occurred during initialization of VM
Too small initial permanent heap