Java Memory Overflow

Source: Internet
Author: User

Java memory is divided into the method area, the virtual machine stack, the local method stack, the heap, the program counter five parts, except the program counter, other parts may appear memory overflow oom (outofmemeryerror).

1. The difference between memory overflow and memory leak
memory Overflow: means that the program does not have enough memory space for it to use when requesting memory.
memory Leak: refers to the program after the application of memory, for some reason can not release the requested memory space, resulting in this block of memory can not be reused, resulting in a waste of system memory. A memory leak hazard can be ignored, but the consequences of memory leak accumulation are serious, no matter how much memory, sooner or later, will be occupied by the light.
2, Memory overflow detailed

2.1.Java virtual machine stack and local method stack

Each line stacks the size of the control parameter when-XSS.

The Java Virtual machine defines two exceptions in the stack, Strackoverflowerror and outofmemoryerror:unable to create new native thread. Strackoverflowerror is thrown when the depth of the request stack is greater than the maximum allowable depth of the Java Virtual machine, and if the Java Virtual machine does not request enough space when the stack is extended, outofmemoryerror:unable to create New native Thread.

Strackoverflowerror: The amount of memory in a single thread exceeds the size of the stack-XSS

Scene appears

A local array or collection is too large. Generally occurs in large queries or large export cases.

Second, there are too many method calls. Generally occurs when there are too many or dead loops in the recursive call hierarchy.

Third, pointer or array out of bounds. This is the most common scenario, such as copying a string, or handling user input, and so on.

How to Solve

optimizing programs, reducing invocation of methods, increasing-XSS parameters

Outofmemoryerror:unable to create new native Thread: The memory consumed by multithreading exceeds the available memory (2G for process free memory (32-bit operating system)-xmx-maxpermsize-the memory that is consumed by the virtual machine itself and the memory used by the program counters)

How to Solve

Reduce the number of threads, reduce the size of the-XSS (that is, reduce the amount of memory each thread has), reduce the size of the-XMX, and expand the space left to the stack MaxPermSize

2. Method Area Memory Overflow

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

Throws Java.lang.OutOfMemoryError:PermGen space when a persistent band overflows

Because class constants and run times are also stored in the method area, too often too many runs can cause oom in the method area, but there are no parameters that directly control the size of the constant pool, which can only be controlled indirectly through-permsize and-maxpermsize.

Scene appears

First, dynamic generation classes such as generatedconstructoraccessor, dynamic proxies, and cglib bytecode enhancement techniques are used in spring and hibernate,mybatis, and strong method areas are needed to support them.

Second, run a constant pool overflow

Third, the use of some application server hot deployment, we will encounter hot deployment a few times after the discovery of memory overflow, this situation is because after each hot deployment, the original class was not unloaded.

How to Solve

One, increase the size of the xx:permsize and xx:maxpermsize parameters in the Java Virtual machine, where xx:permsize is the initial permanent save area size, Xx:maxpermsize is the maximum permanent save area size. For tomcat6.0, add a line to the end of the list of environment variable names in the catalina.sh or Catalina.bat file (about 70 rows):
Java_opts= "-xx:permsize=64m-xx:maxpermsize=128m"
If you are a Windows server, you can also set it in the system environment variable. This memory overflow error can easily occur when using Tomcat to publish Sprint+struts+hibernate schema programs. Using the above method, I successfully solved the problem of frequent outages of the Tomcat server that deployed the SSH project.
Second, clean the jar under Web-inf/lib in the application, if Tomcat deploys more than one application, many applications use the same jar, and the common jar can be moved to the Tomcat common Lib, reducing the repetition load of the class. This method is recommended by some people on the Internet, I have not tried, but feel less space, the most reliable is the first method.

3. Heap Memory Overflow

Heap size is set by-XMS and-XMX

When the heap memory overflows, the virtual opportunity throws Java.lang.OutOfMemoryError:java heap space

Scene appears

If there is no heap memory that can be allocated when creating an object, the JVM throws a Outofmemoryerror:java heap space exception

How to Solve

The first thing you need to distinguish is memory overflow or memory leak.

(1) If it is a memory overflow, the-XMS,-XMX parameter is adjusted by large.
(2) If it is a memory leak, see how the object is referenced by GC Root.

When this happens, we need to specifically analyze the dump file generated when the memory overflows (need to add-xx:+heapdump
Onoutofmemoryerror JVM startup parameters). This can be a memory leak or a memory overflow in the event of this problem.

1. Configuration method

-xx:+heapdumponoutofmemoryerror-xx:heapdumppath=${directory}.
2. Parameter description

(1) The-xx:+heapdumponoutofmemoryerror parameter indicates that the dump file is generated automatically when an oom occurs in the JVM.

(2) The-xx:heapdumppath=${directory} parameter indicates the path to the generated dump file, or you can specify the file name, for example:-XX:HEAPDU
mppath=${directory}/java_heapdump.hprof. If you do not specify a file name, the default is: JAVA_<PID><DATE><TIME>_HEAPDU
Mp.hprof.
If it is a memory leak, we want to find out how the memory leak object is referenced by GC root, and then through the reference chain to specifically analyze the leaked
Reason.

If there is a memory overflow problem, this is often the program native needs more memory than we gave to the virtual machine configuration, in which case I
We can use the big-xmx to solve this problem.

Java Memory Overflow

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.