Monday, November 29, 2010

Unveiling the java.lang.Out OfMemoryError And dissecting Java heap dumps

Unveiling the java.lang.Out OfMemoryError And dissecting Java heap dumps (Page last updated May 2010, Added 2010-11-29, Author Jinwoo Hwang, Publisher ). Tips:

  • A java.lang.OutOfMemoryError (OOME) is thrown when the JVM cannot allocate an object due to memory constraints. There are six different types of memory areas in the JVM: 1. Program Counter Register; 2. Java Virtual Machine Stack; 3. Heap; 4. Method Area; 5. Runtime Constant Pool; 6. Native Memory Stack. All but the first of these can cause an OOME.
  • Different OutOfMemoryErrors are thrown depending on what caused the error and which memory space is unable to provide the required space. The error message usually identfies which memory space has caused the error.
  • "OutOfMemoryError: PermGen space" implies the perm gen space is too small (stores class objects and interned strings); this may be caused by loading too many classes (usually class versions, or generated classes), or interned strings, or the space may be too small (use -XX:MaxPermSize=... to set to a larger value).
  • "OutOfMemoryError: unable to create new native thread" typically happens when you are using very many threads and you have encountered the limitations of the process on that particular machine (possibly at that time). You might want to reduce the stack size of threads to work around this, or examine how further operating system memory resources can be made available to the JVM.
  • "OutOfMemoryError: requested NNN bytes for MMMM. Out of swap space?" Typically occurs when the process has grown very big and you have encountered the limitations of the process on that particular machine (possibly at that time). You might want to reduce the number of objects in the system to work around this, or examine how further operating system memory resources can be made available to the JVM.
  • "OutOfMemoryError: Java heap space" this usually indicates you need to change the maximum size of the heap (-Xmx) or reduce the number of objects being held on to by the application. Can be accopanied by a heap dump if the JVM has been so configured (e.g. with -XX:+HeapDumpOnOutOfMemoryError set).
  • To diagnose an OutOfMemoryError: first examine the error message to determine what caused the error and which memory space was unable to allocate more memory; then determine whether the error can be eliminated woth a configuration change (e.g. larger heap or perm space setting or stack sizes), in some cases it may help you to examine the garbage collection statistics to identify this; finally if this cannot be solved by a configuration change then either the application memory usage needs to be examined (with the help of a heap dump if possible) or the application needs to be spread across multiple JVMs possibly across multiple machines.

No comments: