OutOfMemoryError exception Solution of Java heap Overflow

Source: Internet
Author: User
Tags arrays static class xms

In the description of the Java Virtual Machine specification, in addition to the program counter, there are several other run-time regions of the virtual machine memory that have OutOfMemoryError (called Oom) exception, and below are some examples to verify the occurrence of the exception. In the example below, some of the virtual machine parameters are set in the annotation, and after "VM Args", if you are using a console command to execute the program, put these parameters directly after the Java command on the line, if the use of development tools, the Eclipse IDE, only debug/run the page check settings.


Java Heap Overflow

The heap is used to store object instances when we constantly create objects and ensure that GC roots and objects have a reciprocal referential relationship (GC roots refers to objects of the garbage collector, which are not GC-roots and are not referenced by GC roots), A memory overflow exception is generated when the number of objects reaches the maximum heap capacity.

/**
* VM Args:-xms20m-xmx20m-xx:+heapdumponoutofmemoryerror
* @author Administrator
*
*/
public class Heapoom
{

Static Class oomobject{
}

    public static void Main (string[] args) {
        list< Oomobject> list=new arraylist<oomobject> ();
        while (true) {
             List.add (New Oomobject ());
       }
   }
}
The Java heap is limited to 20MB in the above code, and cannot be extended (the heap's minimum-xms parameter is set to the maximum-xmx parameter to avoid heap automatic expansion), through-xx:+heapdumponoutofmemoryerror Dump the current memory heap dump snapshot for later analysis when the virtual machine has a memory overflow. The results of the operation are as follows:

Java.lang.OutOfMemoryError:Java heap Spaces
Dumping heap to java_pid5504.hprof ...
Heap dump file created [27872304 bytes in 0.150 secs]
Exception in thread "main" Java.lang.OutOfMemoryError:Java Heap Space
    at Java.util.Arrays.copyOf (arrays.java:2245)
    at Java.util.Arrays.copyOf (arrays.java:2219)
    at Java.util.ArrayList.grow (arraylist.java:242)
    at Java.util.ArrayList.ensureExplicitCapacity (arraylist.java:216)
    at Java.util.ArrayList.ensureCapacityInternal (arraylist.java:208)
    at Java.util.ArrayList.add ( arraylist.java:440)
    at Test. Heapoom.main (heapoom.java:20)

Refresh project you can see the snapshot file, you can use Eclipse Memory Analyzer to open and analyze. Determine whether it is memory leak, or memory Overflow. Processing idea: If it is a memory leak, use the tool to further view the leaking object to the GC roots reference chain, find out how the leak object is associated with GC roots and cause the garbage collector to not automatically recycle those objects. You can then determine the location of the code that generated the memory overflow. If there is no memory leak problem, check the virtual machine's heap parameters (-XMS-XMX) compared to the physical machine can also be larger, at the code level to see if there are some objects life cycle is too long, holding state time is too long. Reduce memory consumption during program operation.

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.