Java Virtual Machine Series (iii)---memory overflow situation and solution

Source: Internet
Author: User
Tags xms

Because the Java virtual machine contains heap memory, method area, virtual machine stack, local method stack and program counter five parts, wherein the program counter is the only piece of memory that does not occur the memory overflow exception, so only four memory areas can occur memory overflow exception, Where both the virtual machine stack and the local method stack are memory models executed by the Java method, their exceptions occur almost the same, and in the method area. Another piece of memory is a constant pool, so memory overflow can be divided into Java heap overflow, virtual machine stack and local method stack overflow, method area and run constant pool overflow three cases.

One, Java heap overflow

1, the reason: Because the heap is stored in object instances and arrays, so when the number of objects > maximum heap capacity limit, a memory overflow exception occurs;

2. Solution:

1) If the object is not required, but there is a reference chain that points to GC Root (described later in this section), a memory leak can occur when the GC is not available, and the solution is found in the code by locating the cause of the leak;

2) If the object is necessary, check whether the heap parameters of the virtual machine stack can be adjusted large (when the total memory capacity of the virtual machine is smaller than the physical memory), if you can adjust the size can be resolved by modifying this parameter:

--XMX: Max heap Memory

--XMS: Minimum heap memory

If--xmx and--xms are the same, the heap memory is not dynamically extensible.

Second, virtual machine stack and local method stack Overflow

1, the cause of memory overflow:

Because the virtual machine stack and the local method stack are not distinguished in the hotspot virtual machine, the parameter--xoss setting the local method stack size is invalid, and the stack capacity is generally set by the--XSS parameter (I guess SS is the abbreviation for stack size, so it's better to remember)

Two exception conditions are defined in the virtual machine:

1) A Stackoverflowerror exception occurs when the thread requests a stack depth that exceeds the maximum stack depth allowed by the virtual machine;

2) OutOfMemoryError exception occurs when the stack memory is expanded, if you cannot request enough memory

We know that this part of the memory is thread-private, each thread needs to allocate a piece of memory, so when the thread is a lot of memory overflow occurs, the following analysis of the principle behind this sentence:

① Memory Capacity = Heap memory + method area Memory + program counter memory (negligible) + stack memory (virtual machine stack and local method stack);

② because the stack capacity in the compiler is known, and once the allocation in the runtime will not change, in the case of the stack capacity, each virtual machine stack allocated to the larger stack capacity, the number of threads can be created less;

③ when there are too many threads, it will result in insufficient stack capacity, which can lead to memory overflow.

2. Solution:

First, determine if you can reduce the number of threads, and if it does, reduce the number of threads, and if you cannot reduce the number of threads, you can only resolve by reducing the maximum heap memory capacity and maximum stack capacity:

1)--xmx: Reduce

2)--XSS: Reduce

Iii. method area and run-time constant pool overflow

1. Cause of abnormal occurrence

The method area mainly stores class related information, such as class, name, Access modifier, Chang, field description information, method description information, etc., so if the runtime produces a large number of classes to fill the method area, a memory overflow exception can occur. Here are the ways to generate a large number of classes dynamically, generally the following two kinds:

1) using reflection mechanism or dynamic proxy

2) Direct operation of bytecode using Cglib

2. Solution:

By adjusting the method area size parameters--xx:permsize and-xx:maxpermsize limit the method area size, which is not extensible when set to the same value.

In addition to the above three kinds of virtual machine memory overflow situation, there is a native direct memory overflow, can be specified by the tuning parameter-xx:maxdirectmemorysize, if not specified, and Java heap memory size.

These are some of the memory overflow scenarios and workarounds in the Java Virtual machine.

Java Virtual Machine Series (iii)---memory overflow situation and solution

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.