Detailed JVM memory overflow (stack overflows, heap overflows, lasting generations overflow, can not create a local thread)

Source:   http://www.jianshu.com/p/cd705f88cf2a

 

1, the difference between memory overflows and memory leaks

  Memory overflow (Out Of Memory) : refers to the program at the time of application memory, there is not enough memory space for its use, Out Of Memory appears.

  Memory leaks (Memory Leak) : refers to the program after the application memory, for some reason unable to free up memory space has applied, resulting in this memory can not be used again, resulting in a waste of system memory.

  memory leak will eventually lead to out of memory.

  

2, memory overflow classification

2.1 stack memory overflow (StackOverflowError):

  Program stack depth required is too large, you can write a recursive procedure to trigger dead.

2.2 heap overflow (OutOfMemoryError: java heap space)

Need to distinguish is out of memory or a memory leak:
(1) If a memory overflow, by transfer large -Xms, -Xmx parameter.
(2) If it is a memory leak, you see how the object is referenced GC Root.

2.3 with persistent memory overflow (OutOfMemoryError: PermGen space)

The method comprises the persistent band region, the method comprising the constant pool area.

Therefore, there may be long-lasting and overflow (1) runtime constant pool overflow , there may be (2) preservation method area Class object is not timely recovery of lost or Class information memory occupied exceeded our configuration.

() Trigger constant pool overflow with String.intern.
Class object is not released, the Class object take up too much information, there are too many Class object. It can result in lasting memory with overflow.

2.4 Unable to create native thread

Caused by: java.lang.OutOfMemoryError:unable to create new native thread

The total memory capacity of the system is unchanged, heap, non-heap memory is set too high will lead to less give threads allocated memory.

 

3, memory overflow Detailed

3.1 stack overflow (StackOverflowError)

  Stack overflow throw StackOverflowError error, because such a situation occur when the method of operation, the depth of the stack exceeds the maximum allowable depth of virtual machines due. Under normal circumstances due to procedural errors, such as writing an infinite recursion, it may cause this situation. We adopted the following piece of code to simulate what such a memory overflow condition.

import java.util.*;    
import java.lang.*;    
public class OOMTest{     
    public void stackOverFlowMethod(){    
        stackOverFlowMethod();    
    }    
    public static void main(String[] args){    
        OOMTest oom = new OOMTest();    
        oom.stackOverFlowMethod();    
    }    
}    

Run the above code, we will throw the following exception:

Exception in thread "main" java.lang.StackOverflowError    
        at OOMTest.stackOverFlowMethod(OOMTest.java:6)   

For the stack memory overflow, according to "Java Virtual Machine Specification" Chinese version:

  If the thread requests stack capacity of more than stack the maximum capacity allowed, then, the Java virtual machine will throw a StackOverflow abnormal; if the Java virtual machine stack can be dynamically expanded, and extended action has been tried, but can not apply to enough memory to complete extension, or when there is not enough memory in the newly created thread to create the corresponding virtual machine stack, then the Java virtual machine will throw an OutOfMemory exception.

3.2 heap overflow (OutOfMemoryError: java heap space)

Heap overflow when the virtual machine throws java.lang.OutOfMemoryError: java heap space.

When such a situation occur, dump files based on when we need to produce memory overflow to the specific analysis (need to increase the -XX: + HeapDumpOnOutOfMemoryError jvm startup parameters). Such problems arise when there might be a memory leak, there may be a memory overflow.

1 , configuration

 -XX: + HeapDumpOnOutOfMemoryError -XX: HeapDumpPath = $ { } directory.
2 , parameter description 

( . 1 ) -XX: + HeapDumpOnOutOfMemoryError JVM occurs when the parameter indicates the OOM, DUMP automatically generated file. 

( 2 ) -XX: HeapDumpPath = $ {} parameter indicates the directory path to generate DUMP file may specify the file name, for example: -XX: HeapDumpPath = $ {directory} /java_heapdump.hprof. If you do not specify a file name, the default is: java_ <pid> < DATE > < Time > _heapDump.hprof.

If it is a memory leak, we want to find an object is how memory leaks be quoted GC ROOT, and then through the chain of references to specific analysis of the cause leakage.

If there is a memory overflow problem, which often require more memory than Bunsen program memory to the virtual machine configuration we, in this case, we can use to transfer large -Xmx to solve this problem.

Let's look at the following code demonstrates through this overflow situation:

import java.util.*;    
import java.lang.*;    
public class OOMTest{    
        public static void main(String[] args){    
                List<byte[]> buffer = new ArrayList<byte[]>();    
                buffer.add(new byte[10*1024*1024]);    
        }    
} 

We run the above code through the following command:

java -verbose:gc -Xmn10M -Xms20M -Xmx20M -XX:+PrintGC OOMTest

Program outputs the following information:

[GC 1180K->366K(19456K), 0.0037311 secs]    
[Full GC 366K->330K(19456K), 0.0098740 secs]    
[Full GC 330K->292K(19456K), 0.0090244 secs]    
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space    
        at OOMTest.main(OOMTest.java:7)    

  As seen from the results of operation, the JVM Major gc conducted twice and once Minor gc can be seen from the output of the Major gc, GC area after old 134K was used, and as a byte array 10M, is greater than the combined old generation (years old) space, so an exception is thrown, if the adjustment -Xms21M, -Xmx21M, then it will not trigger abnormal operating gc will not appear.

  In fact, the above is also verified by experiments from the side of a conclusion: the object is larger than the new generation of the remaining memory time, directly into the old era, when the remaining years old memory still can not put down, triggering garbage collection after collection or just can not put down a memory overflow exception will be thrown.

3.3 Persistence and overflow (OutOfMemoryError: PermGen space)

We know Hotspot jvm achieved through sustained with the method area the Java Virtual Machine specification, and runtime constant pool is stored in the method area.

Therefore, there may be long-lasting and overflow:

(1) runtime constant pool overflow .
(2) the method area saved Class object is not timely recovery of lost or Class information memory occupied exceeded our configuration .

When durable and overflow throws java.lang.OutOfMemoryError: PermGen space. Some may appear in the following scenarios:

  • Some applications use server when hot deployment, hot deployment times we will encounter later discovered a memory overflow, this is because after each hot deployment, the original Class not uninstall.

  • If the application itself is relatively large, libraries are more involved, but our lasting memory allocated to the band (by -XX: PermSize and -XX: to set MaxPermSize) is also relatively small when such problems occur.

  • Some third-party frameworks, such as spring, hibernate enhancements are achieved by the technical features generated byte code (such CGLib), this may require a larger area for storing method Class dynamically generated files.

  • We know that Java is placed in string constants constant pool, String.intern () When this method is running, the constant pool will check whether there is an equal and this string object, if there is a direct object literal returned to the pool reference does not exist, this first string constant pool was added, and then returns a reference string.

Then we can be simulated by String.intern method at runtime constant overflow area (under JDK6, because jdk6 in constant pool located PremGen district, the constant pool after jdk7 moved to the Java heap area). Let's simulate this situation through the following code:

import java.util.*;    
import java.lang.*;    
public class OOMTest {    
        public static void main(String[] args) {    
                List<String> list = new ArrayList<String>();    
                while(true){    
                        list.add(UUID.randomUUID().toString().intern());    
                }    
        }        
}    

We run the above code via the following command:

java -verbose:gc -Xmn5M -Xms10M -Xmx10M -XX:MaxPermSize=1M -XX:+PrintGC OOMTest

After the operation console output shown below:

Exception in thread "main" java.lang.OutOfMemoryError: PermGen space    
        at java.lang.String.intern(Native Method)    
        at OOMTest.main(OOMTest.java:8)   

Through the above code, we successfully simulated the situation runtime constant pool overflow from the output of PermGen space can be seen is really happening with persistent overflow, which also verified, we said earlier Hotspot jvm achieved by bringing lasting saying the method area.

3.4 Unable to create native thread

java.lang.OutOfMemoryError: unable to create new native thread

Finally, we take a look at java.lang.OutOfMemoryError: unable to create new native thread this error. When this happens, usually the following two situations:

  • The number of threads created program exceeds the limit of the operating system. For Linux systems, we can -u to view this restriction by ulimit.

  • Memory allocated to the virtual machine is too large, resulting in too little need to create a thread when the native memory.

Build each thread requires stack space allocated to this thread.

We all know that the operating system memory per process is limited, we have to start jvm, the equivalent of starting a process, a process that if we take up 4G of memory, then calculated by the following formula remaining memory is to build thread when you can stack with memory.

Total = 4G thread stack memory available - (- Xmx value) - (-XX: MaxPermSize value) - the memory occupied by the program counter

We can see by the above formula, the greater the -Xmx and MaxPermSize value, the smaller the space for the thread stack available, in the case of the configuration parameters of -Xss stack capacity unchanged, can also create a number of threads smaller. So if this situation is caused because unable to create native thread, then either we increase the total memory occupied by the process, or to reduce the -Xmx or -Xss create more threads to achieve the purpose.

 

Guess you like

Origin www.cnblogs.com/myseries/p/12079757.html