Jvm heap overflow and stack overflow
One, jvm heap overflow
1 Introduction
When running a java program in jvm, if the memory required by the program is greater than the system's maximum heap memory (-Xmx), a heap overflow problem will occur.
2. Case
// Introduction: The execution of this code requires more than 10m of memory space public class HeadOverflow { public static void main(String[] args) { List<Object> listObj = new ArrayList<Object>(); for(int i=0; i<10; i++){ Byte[] bytes = new Byte[1*1024*1024]; listObj.add(bytes); } System.out.println("添加success"); } } // Set the jvm parameter information of the program -Xms1m -Xmx10m -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError The initial heap memory and the maximum available heap memory Gc detailed log information
3. Summary
In the formal project deployment environment, the program reads the system memory by default. Generally, the initial heap memory (-Xms) of the program is set to == the maximum available heap memory (-Xmx).
Two, jvm stack overflow
1 Introduction
a. The stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine StackOverflowError
b. When the virtual machine expands the stack depth, it cannot apply for enough memory space. OutOfMemoryError
Understanding: Each method call will have a stack frame pushed into the virtual machine stack. The operating system allocates a limited amount of memory to the JVM, and the JVM allocates a limited amount of memory to the "virtual machine stack". If there are too many method calls, the virtual machine stack will overflow when the stack is full. Here the stack depth refers to the number of stack frames. https://www.cnblogs.com/lovlife/articles/12452502.html
2. Case
// Loop recursive call, has reached the maximum depth of jvm public class StackOverflow { private static int count; public static void count(){ try { count++; count(); } catch (Throwable e) { System.out.println("Max depth:"+count); e.printStackTrace (); } } public static void main(String[] args) { count(); } }
Adjust the jvm stack size
C:\Users\rocky fang\Documents\mycode>java -Xss2m -cp "C:\Users\rocky fang\Documents\mycode" JavaStackTest
java.lang.StackOverflowError
stack height:23345
C:\Users\rocky fang\Documents\mycode>java -Xss5m -cp "C:\Users\rocky fang\Documents\mycode" JavaStackTest
java.lang.StackOverflowError
stack height:93213
C:\Users\rocky fang\Documents\mycode>java -Xss10m -cp "C:\Users\rocky fang\Documents\mycode" JavaStackTest
java.lang.StackOverflowError
stack height:423618
Set-Xss5m to call after setting the maximum call depth
Summary: Every computer will have a limit maximum call depth to avoid infinite loops of recursion in the code.
The more contents of the local variable table, the larger the stack frame and the smaller the stack depth.
Three, memory overflow and memory leak
1. Differences
: memory overflow: apply for memory space, exceeding the maximum heap memory space.
Memory leak: In fact, it includes memory overflow. The heap memory space is occupied by useless objects and is not released in time, which leads to the occupation of memory and ultimately to memory leaks.
Situation: statically modified objects.
Solution: reduce the definition of constants (see the server memory for details)