out of memory （Out Of Memory） Often referred to as OOM, stay jvm It is mainly divided into method area 、 Pile up 、 Stack 、 Native Method Stack 、 These parts of the program counter , The program counter is the only one that will not appear OOM Of , In other words, other areas will appear OOM. Let's explain the appearance of several areas OOM The situation and characteristics of .
1. java Heap overflow （heap）
Java Heap memory master Use To store during operation all The object of , The area OOM different Chang Yi Generally, there will be the following error message ：
java.lang.OutofMemoryError:Java heap space
Can pass dump Can be analyzed by taking a snapshot of memory , It's the memory leak caused by the program , Or because it's not estimated JVM Memory overflow caused by the size of memory .
2. Stack overflow （stack）
Stack use To store the thread's local variable table 、 The stack of operands 、 Dynamic links 、 Method exit information . If the depth of the request stack is not foot The error that is thrown when is called contains something like below Information about ： java.lang.StackOverflowError.
Since the memory occupied by each thread is about 1M, So thread creation also needs memory space . If the number of threads applied for creation exceeds the remaining memory , A similar error will be thrown as follows ：java.lang.OutofMemoryError: unable to create new native thread
And stack dependent JVM Parameters have ：
1、 -Xss: Stack size per thread ,JDK5.0 After that, the stack size of each thread is 1M, Previously, the stack size of each thread was 256K.
2、 In the same physical memory , Decreasing this value can raw Into more threads . But the operating system is very important to 1 There is a limit to the number of threads in a process , Can't generate... Indefinitely .
There is a stack overflow Possible causes ：
recursive ： Local variables used in recursion are stored in the stack , Stack access efficiency is high , Fast , But the space is limited , Too many recursive variables need to be put on the stack all the time , As a result, the required memory space is larger than the stack space , Stack space is 2M, Heap space, memory space .
Run time constant overflow （constant）
Runtime constants are stored in the method area , It mainly stores various literal quantities and symbol references generated by the compiler , But it's also possible to put new constants into the pool during runtime , such as String Class intern Method . If this area OOM, The error result will contain information similar to the following ：
1 java.lang.OutofMemoryError: PermGen space
dependent JVM Parameters have ：
1、 -XX:PermSize： Set persistent generation (perm gen) Initial value , The default is physical memory 1/64
2、 -XX:MaxPermSize： Set persistent generation maximum , Default to physical memory 1/4
Method area overflow
Method area mainly stores class information loaded by virtual machine , Such as class name 、 Access modifier 、 Constant pool 、 Field description 、 Method description, etc. . In theory JVM The size of this area should be stable after startup , But a lot of frameworks right now , such as Spring and Hibernate Class will be generated dynamically in the process of running , So there are also OOM The risk of . If this area OOM, The error result will contain information similar to the following ：
java.lang.OutofMemoryError: PermGen space
dependent JVM Parameters have ：
1 -XX:PermSize： Set persistent generation (perm gen) Initial value , The default is physical memory 1/64
2 -XX:MaxPermSize： Set persistent generation maximum , Default to physical memory 1/4
more Java Interview questions , Searchable applets ：IT Interview exercises