Java Exception: "java.lang.OutOfMemoryError: Java heap space"

This section provides a tutorial example on how JVM expands the Heap space to allocate more objects and leads to the 'java.lang.OutOfMemoryError: Java heap space' Java exception, if it is not allowed to expand any more.

First, let's use the following simple program, HeapMemoryCrash.java, to observe what will happen when the Heap runs out of memory:

/* HeapMemoryCrash.java
 * Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
class HeapMemoryCrash {
   public static void main(String[] a) {
      int max = 1000;
      Object[] arr = new Object[max];
      heapCheck();
      for (int n=0; n<max; n++) {
         arr[n] = new long[10*1024*128];
         System.out.println((n+1)+": "
            +((n+1)*10)+" MB of objects created.");
         heapCheck();
      }
   }
   public static void heapCheck() {
      Runtime rt = Runtime.getRuntime();
      rt.gc();
      long total = rt.totalMemory();
      long free = rt.freeMemory();
      long used = total - free;
      java.io.Console con = System.console();
      con.format("Total memory: %s%n",total);
      con.format(" Free memory: %s%n",free);
      con.format(" Used memory: %s%n",used);
      String str = con.readLine("Press ENTER key to continue: ");
   }
}

This program is designed to:

Compile and run with HotSpot JVM 1.8:

C:\herong>\progra~1\java\jdk1.8.0\bin\javac HeapMemoryCrash.java

C:\>\progra~1\java\jdk1.8.0\bin\java -Xms20m -Xmx50m HeapMemoryCrash
Total memory: 20447232
 Free memory: 20188720
 Used memory: 258512
Press ENTER key to continue:
1: 10 MB of objects created.
Total memory: 26152960
 Free memory: 15377464
 Used memory: 10775496
Press ENTER key to continue:
2: 20 MB of objects created.
Total memory: 50724864
 Free memory: 29463808
 Used memory: 21261056
Press ENTER key to continue:
3: 30 MB of objects created.
Total memory: 50724864
 Free memory: 18978032
 Used memory: 31746832
Press ENTER key to continue:
4: 40 MB of objects created.
Total memory: 50724864
 Free memory: 8492256
 Used memory: 42232608
Press ENTER key to continue:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
   at HeapMemoryCrash.main(HeapMemoryCrash.java:10)

The output confirms that:

Now rerun the example with a higher Heap size to see how much space the operating system allows you to give the JVM:

C:\herong>\progra~1\java\jdk1.8.0\bin\java -Xms1550m HeapMemoryCrash
Total memory: 1571160064
 Free memory: 1570902744
 Used memory: 257320
Press ENTER key to continue:
...

C:\herong>\progra~1\java\jdk1.8.0\bin\java -Xms1560m HeapMemoryCrash
Error occurred during initialization of VM
Could not reserve enough space for 1597440KB object heap

This tells me that my 32-bit Windows system only allows my specify up to 1,550 MB for the JVM heap space.

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 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 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

Memory Management and Garbage Collectors

 Memory Management General Rules

Java Exception: "java.lang.OutOfMemoryError: Java heap space"

 OutOfMemoryError Comparison of HotSpot and JRockit

 Garbage Collection Demonstration

 JVM Memory Manager - Garbage Collector

 Generational Garbage Collection in HotSpot

 Young Generation Collection - Minor Collection

 Tenured Generation Collection - Full Collection

 HotSpot Default Garbage Collector - Serial Collector

 "-XX:+PrintGCDetails" - Garbage Collection Logging

 GC Log Messages on GarbageCollection.java

 Serial, Parallel, Concurrent, and Regionalized Collectors

 Parallel Collector GC Log Message Format

 Parallel Compacting Collector GC Log Message Format

 Concurrent Mark-Sweep Collector GC Log Message Format

 Garbage First GC Log Message Format

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 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

 Outdated Tutorials

 References

 PDF Printing Version