OutOfMemoryError and JVM memory structures in Java

Source: Internet
Author: User
Tags xms

OutOfMemoryError in the development process is commonplace, encountered this error, novice programmers know from two aspects to solve:

1: is a bug that causes a memory leak;
2: is to adjust the JVM startup parameters to increase memory.

OutOfMemoryError There are several cases, each time you encounter this error, observe the outofmemoryerror behind the message, you can find the difference, such as:
ReferencesJava.lang.OutOfMemoryError:Java Heap Space
Java.lang.OutOfMemoryError:unable to create new native thread
Java.lang.OutOfMemoryError:PermGen Space
java.lang.OutOfMemoryError:Requested array size exceeds VM limit
Although all called outofmemoryerror, but the cause behind each error is not the same, the solution also depends on the situation, can not be generalize. Only in-depth understanding of the JVM's memory structure and careful analysis of the error message, it is possible to do the remedy, Shoudaobingchu.

The JVM Specification JVM specification defines several blocks of memory for the Java runtime, which are:

    • JVM Stack (Java Virtual machine Stacks),
    • Heap (heap),
    • Method Area,
    • Chang (Runtime Constant Pool),
    • Local methods Stack (Native method Stacks),

However, the memory layout and address space for each block area are not clearly defined, leaving the space for each JVM vendor to play.

Hotspot Jvmsun's own hotspot JVM implements a relatively clear description of the heap memory structure. Follow the implementation of the hotspot JVM:

Heap memory is divided into 3 generations:

    • Young Generation,
    • Old (tenured) Generation,
    • Permanent Generation.

It is well known that GC (garbage collection) is the three generation of heap memory. Young is used to allocate new Java objects, which are then divided into three parts: Eden space and two survivor space (called From and to), which are used to store objects that survived the GC process, The permanent is used to store metadata such as class classes loaded by the JVM. See the Hotspot Memory Management whitepaper for details. The layout diagram of the heap is as follows:







Based on this information, we can derive the corresponding relationship between the memory partition of the JVM specification and the memory area in the hotspot implementation: The heap of the JVM specification corresponds to the young and old Generation, and the method area and constant pool correspond to permanent Generation. The hotspot implementation is not detailed for stack memory, but the hotspot white paper mentions that the Java line stacks is represented by the stack and threading model of the host operating system, and the Java method and the native method share the same stack. Therefore, it can be considered that the JVM stack and the local method stack are one thing in a hotspot.



Through the above foreshadowing, it can be learned that the memory of the JVM process is roughly divided into heap space and stack space two parts. Heap is divided into young, old (tenured), permanent three generations. Stack is divided into Java method stack and native method stack (do not differentiate), in the stack memory area, you can create multiple lines stacks, each line stacks occupy a small portion of the stack area of memory, the line stacks is a LIFO data structure, each call a method, will create a frame at the top of the stack, When the method returns, the corresponding frame is removed from the top of the stack (by moving the top pointer of the stack). Overflow errors can occur in each part of the memory. Back to the beginning of the OutOfMemoryError, the following describes the cause of the error and the resolution (each outofmemoryerror may be a program bug, so the workaround does not include troubleshooting the bug).

Java.lang.OutOfMemoryError:Java Heap Space

Cause: heap memory overflow means that there is not enough memory for young and old generation.
Workaround: Adjust the java boot parameter-xms-xmx to increase heap memory.

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

Cause: Stack space is not enough to create additional threads, either too many threads are created, or the stack space is indeed small.
Workaround: Because the JVM does not provide parameters to set the total stack space size, but can set the size of a single line stacks, and the system's user space is a total of 3G, in addition to text/data/bss/memorymapping several paragraphs, heap and stack space is limited, It is the one that has been eliminated. As a result, there are two ways to solve this error:

    • 1. Reduce the size of a single thread stack by-XSS startup parameters, so that more threads can be opened (not too small, too small to appear stackoverflowerror);
    • 2. Reduce the heap size by-xms-xmx two parameters and give the memory to the stack (provided that the heap space is sufficient).

Java.lang.OutOfMemoryError:PermGen Space

Cause: Permanent generation space is insufficient to load additional classes.
Workaround: Adjust-xx:permsize=-xx:maxpermsize= two parameters to increase PermGen memory. In general, these two parameters do not set manually, as long as the settings-xmx large enough, the JVM will choose the appropriate PermGen size. For spring,hibernate these frameworks that require dynamic type support, this area requires sufficient space.

java.lang.OutOfMemoryError:Requested array size exceeds VM limit

Cause: This error is relatively rare (try new one with a length of 100 million), also due to insufficient heap space. If a new array of this size is needed, the logic of the program is mostly unreasonable.
FIX: Modify the program logic. Or, you can increase heap memory by-XMX.

When the GC spends a lot of time, but only a small amount of memory is recycled, OutOfMemoryError is also reported, and I've only met one or two times. When using the-XX:+USEPARALLELGC or-XX:+USECONCMARKSWEEPGC collector, an error is given in the above case, as described in the hotspot GC turning documentation:
The parallel (concurrent) collector would throw an outofmemoryerror if too much time be being spent in garbage collection:i F more than 98% of the total time was spent in garbage collection and less than 2% of the heap are recovered, an OutOfMemory Error would be thrown.
For this problem, one needs to do GC turning, and the other is to optimize the program logic.

Java.lang.StackOverflowError

Cause: This is also a memory overflow error, that is, the overflow of the line stacks, either the method call hierarchy too many (such as there is an infinite recursive call), or the line stacks is too small.
Solution: Optimize program design, reduce method call level, adjust-XSS parameter to increase thread stack size.


The heap in the JVM is divided into 3 large intervals, and the JVM provides some options to control the size of the young,tenured.

-XMS: Specifies that the JVM initializes memory after initial startup
-XMN: Parameter sets the size of the young generation
-XMX: Specifies that the JVM heap the maximum memory, and after the JVM is started, allocates the-XMX parameter with the specified size of memory to the JVM, but not necessarily all, the JVM adjusts the memory that is actually used for the JVM based on the-XMS parameter

-xx:newratio=8 means that the ratio of tenured to young is 8:1, so EDEN+2*SURVIVOR=1/9
-xx:survivorratio=32 means that the ratio of Eden to a survivor is 32:1, so a survivor accounts for 1/34 of the young area.
-xx:permsize=16m-xx:maxpermsize=64m

Thread Stack
-xx:xss=128k

OutOfMemoryError and JVM memory structures in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.