Common memory overflow in java
VM Stack Overflow (if the VM cannot apply for sufficient memory space during expansion, an OutOfMemoryError will be thrown)
Package com. jvm. memory; import java. util. arrayList; import java. util. list; public class HeapOOM {/*** VM runtime parameter-Xms20m-Xmx20m-XX: + HeapDumpOnOutOfMemoryError * @ param args */public static void main (String [] args) {List
List = new ArrayList
(); 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 heap is used to store object instances. As long as we constantly create objects and reach the maximum capacity limit, the heap memory will overflow,
If this happens in the system, we need to analyze the snapshots dumped by dump using the image analysis tool to see if these classes have been created.
Required. Check from two aspects: 1. check whether there is a problem with the parameter settings and whether it can be increased. 2: Check whether the lifecycle of some objects is too long.
Local method Stack Overflow (if the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine itself, StackOverflowError will be thrown)
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)
Constant pool overflow
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
list = new ArrayList
();int i=0;while(true){list.add(String.valueOf(i++).intern());}}}
Error occurred during initialization of VM
Too small initial permanent heap