OutOfMemoryError Analysis in hotspot

Source: Internet
Author: User
Tags gc overhead limit exceeded jconsole

There are 3 types of memory in the JVM: Heap (heap memory), Non-heap (non-heap memory) [3], and native (local memory). [1]

Heap memory is an area of memory where all class instances and arrays are allocated at run time. Non-heap memory contains the memory required to process or optimize the method area and the JVM, storing class structures such as run-time pools, fields and method structures, and methods and constructor code. Local memory is virtual memory managed by the operating system. An java.lang.OutOfMemoryError exception is thrown when an application is running out of memory. [1]

Problem Representation Diagnostic tools
Not enough memory OutOfMemoryError Java Heap Analysis Tool (jhat) [4]
Eclipse Memory Analyzer (MAT) [5]
Memory leaks Use memory growth, frequent GC Java Monitoring and Management Console (jconsole) [6]
JVM Statistical Monitoring Tool (Jstat) [7]
A class has a large number of instances Memory Map (jmap)-"Jmap-histo" [8]
object is mistakenly referenced Jconsole [6] or jmap-dump + jhat [8][4]
Finalizers Object Wait End Jconsole [6] or jmap-dump + jhat [8][4]

OutOfMemoryError will be thrown when the Java process fails to allocate enough memory to run:

1. Java.lang.OutOfMemoryError:Java Heap Space
When the heap memory overflows, first determine what the current maximum memory is (parameter: -xmxOr -xx:maxheapsize=), you can use the command Jinfo-flag maxheapsize[9] View the configuration of the running JVM, if the value is already large, you should find the problem through a tool such as mat [5], or Jmap-histo[8] Find out which class or classes are consuming more memory. Parameters -VERBOSE:GC (-XX:+PRINTGC)-xx:+printgcdetailsYou can print some GC-related data. If the problem is more difficult to troubleshoot can also be through the parameters -xx:+heapdumponoutofmemoryerrorDump memory data before the Oom is analyzed again. This problem can also be printed by Histodiff multiple memory histogram before the difference, to help see which classes too many are instantiated, if too many of the instantiated classes are positioned after the btrace can be tracked again. [1] [2]
The following code can reproduce the exception: [2]
List<string>=newArrayList<string> ();
While(true) list. Add(newString("Consume more memory!" ));
2. Java.lang.OutOfMemoryError:PermGen Space
PermGen SpaceA permanent generation, an area of non-heap memory. The main data is the class structure and calls the intern ()The string. [2]
List<Class<?>>Classes= New ArrayList<Class<?>> ();
While(True){
MyclassloaderCl= New Myclassloader();
    try{
        Classes. (cl. Loadclass "Dummy"     }catch ( classnotfoundexception E) {
        E. Printstacktrace     }
}
A class-loaded log can track the load of a class through Btrace:
ImportCom.Sun.Btrace.Annotations.*;
Import StaticCom.Sun.Btrace.Btraceutils.*;

@BTrace
Public Class Classloaderdefine {

@SuppressWarnings("Rawtypes")
@OnMethod(Clazz= "+java.lang.classloader",Method= "DefineClass",Location= @Location(Kind.RETURN))
Public Static voidOnclassloaderdefine(@Return ClassCl {
        println< Span class= "pun" > ( "= = = Java.lang.classloader#defineclass = ="         Println (strings. Strcat ( "Loaded class:" , Reflective. Namecl         Jstack10     }
}
In addition to Btrace, you can open the parameters of the log load to see which classes are loaded, and you can set the parameters -xx:+traceclassloadingOpen, or Use parameters -verbose:class( -xx:+traceclassloading,-xx:+traceclassunloading), you can see in the log output which classes are loaded into the Java virtual machine. This parameter can also be used by the Jflag command Java-jar Jflagall.jar-flag +classverboseDynamic Open -verbose:class。
The following is a use of a String.intern ()Example of: [2]
list<string> List = new arraylist< Span class= "pun" ><string> ();  int I=0; 
while (true) list< Span class= "pun". add (( "consume more memory!" + (i++)).
You can find the class call by using the following Btrace script:
ImportCom.Sun.Btrace.Annotations.*;
Import StaticCom.Sun.Btrace.Btraceutils.*;

@BTrace
Public Class Stringinterntrace {

@OnMethod(Clazz= "/.*/",Method= "/.*/",
Location= @Location(Value= Kind.Pager,Clazz= "Java.lang.String",Method= "Intern"))
Public Static voidM(@ProbeClassName StringPcm, @ProbeMethodName StringProbemethod,
@TargetInstance object Instance) {< Span class= "PLN" >
        Println (strcat (pcm, Strcat ( "#" , Probemethod
        Println (strcat ( ">>>>" , str instance
    }
}
3. java.lang.OutOfMemoryError:unable to create new native thread
Each boot thread in the JVM allocates a piece of local memory to hold the thread's call stack, which is freed only at the end of the line. This error occurs when there is not enough local memory to create the thread. The problem can easily be reproduced by the following code: [2]
While(True){
New Thread(New Runnable(){
Public voidRun() {
Try {
Thread. Sleep(* *);
} catch(interruptedexception e) { }
}
}). start();
}< /c10>
4. java.lang.OutOfMemoryError:Direct Buffer Memory
That is, allocating memory from the direct memories fails, the direct buffer object is not allocated on the heap, it is allocated in direct memory and is not managed directly by the GC (but the Java object of direct buffer is managed by GC, As long as the GC reclaims its Java object, the operating system frees the space requested by direct buffer. Pass -xx:maxdirectmemorysize=You can set the size of direct memory. [2] [10]
List<bytebuffer>=newArrayList<bytebuffer> ();
While(true) list. Add(bytebuffer. Allocatedirect(10000000));
5. Java.lang.OutOfMemoryError:GC overhead limit exceeded
JDK6 new error type. Thrown when a GC takes up a lot of time to free up a small space. Generally because the heap is too small. Cause of the exception: there is not enough memory. This feature can be turned off by the parameter -xx:-usegcoverheadlimit . []
6. java.lang.OutOfMemoryError:Requested array size exceeds VM limit
the details indicate that the array size of the application request has exceeded the heap size. If the application requests an array of size 512M, but the heap size is only 256M, this will throw OutOfMemoryError, because the virtual machine cannot be exceeded to allocate a new array at this time. In large cases, the heap memory allocation is too small, or the application attempts to allocate an oversized array, such as the algorithm used by the application calculates the error size. []
7. java.lang.OutOfMemoryError:request <size> bytes for <reason>. Out of swap space?
local memory allocation failed. An application's Java Native Interface (JNI) code, local libraries, and Java virtual machines all allocate memory allocation space from the local heap. Throws a OutOfMemoryError exception when memory is allocated from the local heap fails. For example, when both the physical memory and the swap partition are exhausted, an Oufofmemoryerror exception is thrown when you try to allocate memory locally. []
8. java.lang.OutOfMemoryError: <reason> <stack trace> (Native method)
if the details of the exception are <reason> <stack trace> (Native method) and a thread stack is printed, and the topmost frame is the local approach, This exception indicates that the local method has encountered a memory allocation problem. Their difference is that the memory allocation failure is either JNI or local method discovery or Java Virtual Machine discovery compared to the previous exception. []
Resources

[1].http://java.sun.com/developer/technicalArticles/J2SE/monitoring/
[2].http://eyalsch.wordpress.com/2009/06/17/oome/
[3].Http://docs.oracle.com/javase/6/docs/api/java/lang/management/MemoryType.html
[4].Http://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html
[5].http://www.eclipse.org/mat/
[6].Http://docs.oracle.com/javase/6/docs/technotes/tools/share/jconsole.html
[7].Http://docs.oracle.com/javase/6/docs/technotes/tools/share/jstat.html
[8].Http://docs.oracle.com/javase/6/docs/technotes/tools/share/jmap.html
[9].Http://docs.oracle.com/javase/6/docs/technotes/tools/share/jinfo.html
[ten].http://eyesmore.iteye.com/blog/1133335
[One by one ].Http://www.oracle.com/technetwork/java/javase/index-137495.html
[].Http://www.oracle.com/technetwork/java/javase/memleaks-137499.html
[].http://blog.csdn.net/forandever/article/details/5717890

OutOfMemoryError Analysis in hotspot

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.