Java memory overflow positioning and solutions

Source: Internet
Author: User

In the previous section, the Java Virtual Machine memory distribution said that the Java virtual machine is divided into five regions, and also know that in the Java Program Counter area does not appear oom (Outofmemeryerror), The following is an explanation of the principle of oom in four regions except for the program counter and a solution.


1.Java virtual machine stack and local method stack

The size of the stack controls the number of parameters when-XSS.

The Java Virtual machine defines two exceptions, Strackoverflowerror and Outofmemeryerror, in the stack. Strackoverflowerror is thrown when the depth of the request stack is greater than the maximum agreed to by the Java Virtual machine, assuming that the Java virtual machine is in the stack extension. When there is not enough space to apply, the Outofmemeryerror is thrown.

Strackoverflowerror:java virtual machine in the execution, when calling the method, all create the stack frame, when the stack of space is not enough to produce strackoverflowerror. Then the corresponding solution can only be adjusted-XSS parameters, or reduce the method of call, reduce the size of the stack frame two ways.

Outofmemeryerror: An oom on the stack is usually a multithreaded scenario.

First of all, let's analyze how much space the stack can use, and take 32-bit operating systems for example. Maximum memory 2g-xmx (maximum heap capacity)-maxpermsize (most generous area capacity)-memory that is consumed by the virtual machine itself and by program counters.

The rest of the memory is the space the stack can use, and when the parameters of the XSS configuration are certain, the process of creating threads is constant. An oom is thrown when a stack space cannot be applied. Then the corresponding solution is to adjust the-XSS to reduce the stack size, or adjust the size of-xmx and MaxPermSize to the stack space.


2. Method Area Memory Overflow

The size of the method area is controlled by-permsize and-maxpermsize.


Because class constants and execution times are also stored in the method area, too often too much execution can lead to oom in the method area, but there is no direct control of the size of the constant pool, only indirectly through-permsize and-maxpermsize.

In spring and hibernate. MyBatis uses dynamic generation classes such as generatedconstructoraccessor, dynamic proxies, and cglib byte-code enhancement Technology, so a powerful method area is needed to support it.


3. Overflow of heap memory

Heap memory overflow is more complex, you need to adjust the GC and other parameters, we will explain in the later chapters.


Java memory overflow positioning and solutions

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.