Handling out of memory errors in Hotspot

The Hotspot compiler does not have an ExitOnOutOfMemoryError as the JRockit compiler does. You can simulate it on linux with something like what is below.

We start with our test code that will ensure an out of memory condition…

import java.util.*;

public class oom {
  public static void main(String args[]) {
    try {
      int[] a = new int[Integer.MAX_VALUE];
    }
    catch( Error e ) {
      e.printStackTrace();
    }
    String s = "";
    try {
      Thread.sleep(5000);
    }
    catch( Exception e ) {
      e.printStackTrace();
    }
    for (int i = 1; i <= 1000; i++) {
	  s = s + "x";
	}
	ArrayList al = new ArrayList();
	for (int i = 1; i <= 10; i++) {
	  al.add(new String(s));
	}
  }
}

...and the following commmand line and output...

[atg@expressdb1 ~]$ java -XX:OnOutOfMemoryError="jmap %p;kill -9 %p" oom
#
# java.lang.OutOfMemoryError: Java heap space
# -XX:OnOutOfMemoryError="jmap %p;kill -9 %p"
#   Executing /bin/sh -c "jmap 18128"...
Attaching to process ID 18128, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 20.0-b12

using thread-local object allocation.
Parallel GC with 2 thread(s)

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 788529152 (752.0MB)
   NewSize          = 1310720 (1.25MB)
   MaxNewSize       = 17592186044415 MB
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 174063616 (166.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 12320768 (11.75MB)
   used     = 0 (0.0MB)
   free     = 12320768 (11.75MB)
   0.0% used
From Space:
   capacity = 2031616 (1.9375MB)
   used     = 0 (0.0MB)
   free     = 2031616 (1.9375MB)
   0.0% used
To Space:
   capacity = 2031616 (1.9375MB)
   used     = 0 (0.0MB)
   free     = 2031616 (1.9375MB)
   0.0% used
PS Old Generation
   capacity = 525729792 (501.375MB)
   used     = 114680 (0.10936737060546875MB)
   free     = 525615112 (501.26563262939453MB)
   0.02181348703175642% used
PS Perm Generation
   capacity = 21757952 (20.75MB)
   used     = 2789736 (2.6604995727539062MB)
   free     = 18968216 (18.089500427246094MB)
   12.821684687970633% used
#   Executing /bin/sh -c "kill -9 18128"...
Killed
[atg@expressdb1 ~]$

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.