Heap Expansion Demonstration

This section provides a tutorial example to show the expansion of Heap when more objects are allocated. When Heap runs out of space, JVM throws the OutOfMemoryError exception.

Here are my understandings of the Heap data area, based readings available on the Internet:

To validate some of these understandings and demonstrate the Heap usage and expansion, I wrote the following sample program, HeapMemoryCrash.java, forcing the JVM run out of memory on the Heap:

/* 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];
      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.");
   public static void heapCheck() {
      Runtime rt = Runtime.getRuntime();
      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:\herong>\progra~1\java\jdk1.8.0\bin\java HeapMemoryCrash
Total memory: 16318464
 Free memory: 16059952
 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: 51429376
 Free memory: 30168320
 Used memory: 21261056
Press ENTER key to continue:
3: 30 MB of objects created.
Total memory: 76767232
 Free memory: 45020400
 Used memory: 31746832
Press ENTER key to continue:
23: 230 MB of objects created.
Total memory: 259522560
 Free memory: 18080840
 Used memory: 241441720
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:

See other parts of the book for more tutorials on the Heap data area.

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

 What Are Runtime Data Areas?

 Method Area Expansion Demonstration

 OutOfMemoryError on the Method Area

 Method Area Growth with Dynamically Generated Classes

 Garbage Collection Issue with Dynamically Generated Classes

 Interned Strings Stored in Heap

Heap Expansion Demonstration

 Direct Memory Expansion Demonstration

 allocateMemory() Method on Direct Memory

 JVM Stack Expansion Demonstration

 PC Register and Native Method Stack

 Memory Management and Garbage Collectors

 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


 PDF Printing Version