A study of Java OutOfMemory anomalies

Source: Internet
Author: User

Java Heap Overflow

Virtual Machine Parameters:

-xms20m-xmx20m-xx:+heapdumponoutofmemoryerror

Limit the minimum and maximum values of a heap to a 20m,-xx:+heapdumponoutofmemoryerror memory exception a seasonal Java Virtual machine dump heap memory Dump snapshot

Code

1 ImportJava.util.*;2 3 /**4 * Created by Zcy on 2017/6/11.5  */6  Public classTestheapmemory {7 8     Static classoomobject{9 Ten     } One  A      Public Static voidMain (string[] args) { -list<oomobject> list =NewArraylist<oomobject>(); -          while(true){ theList.add (Newoomobject ()); -         } -     } -}

An exception occurred running:

"Main" Java.lang.OutOfMemoryError:Java heap space at    java.util.Arrays.copyOf (arrays.java:3210) At    java.util.Arrays.copyOf (arrays.java:3181) at    java.util.ArrayList.grow (Arraylist.java: 261) at    java.util.ArrayList.ensureExplicitCapacity (arraylist.java:235)    at Java.util.ArrayList.ensureCapacityInternal (Arraylist.java:227) Heap dump file created [28361944 Bytes in 0.134 secs] @    java.util.ArrayList.add (arraylist.java:458)    at Testheapmemory.main (Testheapmemory.java:15)

In order to see the usage of the live heap, we can install VISUALVM Launcher.

Installation please refer to Http://www.oschina.net/translate/setting-up-visualvm-in-under-5-minutes

Using the VISUALVM Launcher debugger (which requires sleep delay in the program), the heap memory is found to rise all the way to the last drop.

View dump file, the vast majority of memory is occupied by the array

To resolve the heap's outofmemory, the general approach is to use the Memory image analysis tool to analyze the heap dump file, with a focus on verifying that the objects in memory are necessary, that is, whether there is a memory leak or a memory overflow.

If it is a memory leak, you can further use the tool to view the reference chain to the GC roots . You can then find out what path the compromised object was associated with GC roots, which prevented the GC from being automatically recycled.

If it is necessary for each object to survive, check that the heap parameters (-XMS and-XMX) can also be adjusted

Virtual machine stack and local method stack Overflow

Two kinds of exceptions:

    • StackOverflow exception: The stack depth of a thread request is greater than the maximum allowed depth of the virtual machine.
    • OutOfMemory exception: The virtual machine expansion stack cannot request enough memory space.

Virtual Machine Parameters:

-xss128k

Code:

1 /**2 * Created by Zcy on 2017/6/11.3  */4  Public classTeststackof {5 6     Private Static intStacklength = 0;7 8      Public Static voidStackleak ()throwsinterruptedexception {9stacklength++;Ten stackleak (); One     } A  -      Public Static voidMain (String args[])throwsThrowable { -         Try{ the teststackof.stackleak (); -         } -         Catch(Throwable e) { -System.out.println ("Stack length is:" +stacklength); +             Throwe; -         } +     } A}

Operation Result:

Stack length is:1102"main" Java.lang.StackOverflowError at    teststackof.stackleak (Teststackof.java :9) at    teststackof.stackleak (Teststackof.java:ten) at    Teststackof.stackleak ( Teststackof.java £ ºat    teststackof.stackleak (teststackof.java:ten) ...        

Finally, summarize:

    • For StackOverflow Exceptions: Stack depth (1000-2000) is sufficient in most cases. There is a stack of errors to read.
    • If a memory overflow is caused by too many threads, you can reduce the heap capacity and the stack capacity of each process in exchange for more threads.

Method area and run-time-constant pool overflow

A study of Java OutOfMemory anomalies

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.