Java Virtual machine OutOfMemoryError exception details and Solutions (3)

Source: Internet
Author: User

is a Java runtime memory map, the virtual machine memory has occurred outofmemoryerror (hereafter called OOM) anomaly, as a qualified Java developer, we should do is:

(1) First, through the code to verify the Java virtual machine specification described in the various runtime areas stored content;
(2) Second, when encountering memory overflow, should be able to find a specific location, and reasonable solution;

Here's a chat. OOM:

One, Java heap overflow

We know that the Java heap is used to store object instances, as long as we constantly create objects and ensure that GC Roots to objects with reachable paths to prevent the garbage collection mechanism from clearing these objects, a memory overflow exception (OOM) is generated after the number of objects reaches the maximum heap capacity limit.

The cases are as follows:

/** * VM Args: -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError * 需要在jre1.5下编译 * @author xuliugen */publicclass HeapOOM {    static class OOMObject{}    publicstaticvoidmain(String[] args){        new ArrayList<OOMObject>();        while(true){            list.add(new OOMObject());        }    }}

In order to be able to see the state information when the memory overflows, right-click on the project –run As–run configuration–arguments

You can then see a burst of errors:

Java.lang.OutOfMemoryError:Java heap spacedumping Heap to java_pid6424. Hprof... Heap dump file created [28129384bytesinch 0.118Secs]exceptioninchThread"Main"Java. Lang. OutOfMemoryError: Java heap space at Java. Util. Arrays. CopyOf(Unknown Source) at Java. Util. Arrays. CopyOf(Unknown Source) at Java. Util. ArrayList. Grow(Unknown Source) at Java. Util. ArrayList. Ensureexplicitcapacity(Unknown Source) at Java. Util. ArrayList. Ensurecapacityinternal(Unknown Source) at Java. Util. ArrayList. Add(Unknown Source) atcom. LC. Oom. Heapoom. Main(Heapoom. Java: +)

The OOM exception for Java heap memory is the most common memory overflow exception in practical applications. When a Java heap memory overflow occurs, the exception stack information "Java.lang.OutOfMemoryError" is followed by a further hint "Java heap space". To solve the anomaly in this area, the general approach is to first analyze the dump-dump snapshot with a memory-image analysis tool (such as Eclipse Memory Analyzer), with a focus on confirming that the in-memory object is necessary, that is, to get a clear picture of the memory leak ( Memory Leak) is also an overflow (Overflow) You can use Eclipsememory Analyzer to open a heap dump snapshot file. (Eclipse Memory Analyzer is an Eclipse plugin, online Baidu, a lot)

In the case of a memory leak, further tools can be used to view the reference chain of the leaking object to the GC Roots. You can then find out what path the leak is associated with GC Roots and cause the garbage collector to not automatically reclaim them. By holding the type information of the leaking object, as well as the information of the GC Roots reference chain, the location of the leak code can be located in a more accurate manner.

If there is no leak, in other words, the objects in memory must still exist, then you should check the virtual machine's heap parameters (-xmx and-XMS), and the machine physical memory to see if it can be larger, from the code to check if there are some object life cycle is too long, holding the state of the time is too long, Try to reduce the memory consumption during program run time.

The above is a brief idea of handling the Java heap memory problem.

Next, talk about learning: virtual machine stack and local method stack Overflow

Java Virtual machine OutOfMemoryError exception details and Solutions (3)

Related Article

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.