JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

Java Exception: java.lang.OutOfMemoryError

This section provides a tutorial example on generating the java.lang.OutOfMemoryError exception by allocating a big array in a loop.

I wrote the following program to try to push JVM for more memory in a loop:

/**
 * MemoryCrash.java
 * Copyright (c) 2002 by Dr. Herong Yang, herongyang.com
 */
class MemoryCrash {
   public static void main(String[] a) {
      int max = 100;
      Runtime rt = Runtime.getRuntime();
      Object[] arr = new Object[max];
      for (int n=0; n<max; n++) {
         System.out.println("Having "+n*10+" MB and adding 10 MB...");
         arr[n] = new long[10*1024*128];
         System.out.println(" Free memory: " + rt.freeMemory());
         System.out.println("Total memory: " + rt.totalMemory());
      	 try {
      	    Thread.sleep(2*1000);
      	 } catch (InterruptedException e) {
            System.out.println("Interreupted...");
      	 }
      }
   }
}

Output on HotSpot Client 1.6.0 VM with -Xms2m -Xmx64m

C:\>\progra~1\java\jdk1.6.0_02\bin\java -Xms2m -Xmx64m 
   MemoryCrash
Having 0 MB and adding 10 MB...
 Free memory: 1907072
Total memory: 12521472
Having 10 MB and adding 10 MB...
 Free memory: 8421336
Total memory: 29528064
Having 20 MB and adding 10 MB...
 Free memory: 6210456
Total memory: 37826560
Having 30 MB and adding 10 MB...
 Free memory: 14484288
Total memory: 56606720
Having 40 MB and adding 10 MB...
 Free memory: 3998512
Total memory: 56606720
Having 50 MB and adding 10 MB...
Exception in thread "main" java.lang.OutOfMemoryError: 
Java heap space at MemoryCrash.main(MemoryCrash.java:12)

Output on HotSpot Client 1.4.0 VM with -Xms2m -Xmx64m

Having 0 MB and adding 10 MB...
 Free memory: 1897344
Total memory: 12521472
Having 10 MB and adding 10 MB...
 Free memory: 8463216
Total memory: 29573120
Having 20 MB and adding 10 MB...
 Free memory: 6275936
Total memory: 37871616
Having 30 MB and adding 10 MB...
 Free memory: 14644240
Total memory: 56725504
Having 40 MB and adding 10 MB...
 Free memory: 4157184
Total memory: 56725504
Having 50 MB and adding 10 MB...
Exception in thread "main" java.lang.OutOfMemoryError

Output on JRockit 7.0 JVM with -Xms2m -Xmx64m

Having 0 MB and adding 10 MB...
 Free memory: 8717040
Total memory: 19419136
Having 10 MB and adding 10 MB...
 Free memory: 8698800
Total memory: 29904896
Having 20 MB and adding 10 MB...
 Free memory: 7607344
Total memory: 39297024
Having 30 MB and adding 10 MB...
 Free memory: 6951960
Total memory: 49127424
Having 40 MB and adding 10 MB...
 Free memory: 6558720
Total memory: 59219968
Having 50 MB and adding 10 MB...
java.lang.OutOfMemoryError

Note that

  • My 10MB array is slightly more than 10MB because of the array overhead.
  • I also tested with JRockit with -Xmx512m, it got 200MB before running out of memory. My system has: 128MB physical memory, and 288MB paging file size.

Last update: 2002.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

Memory Management Rules and Tests

 Memory Management General Rules

Java Exception: java.lang.OutOfMemoryError

 Garbage Collection Process

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
Java Exception: java.lang.OutOfMemoryError