JVM Stack Expansion Demonstration

This chapter provides tutorial notes and example codes on JVM runtime data areas. Topics include introduction of JVM runtime data areas: Method Area, Heap, Direct Memory, PC Registers, JVM Stacks, and Native Method Stacks; different between thread-shared and per-thread data areas; demonstration programs for different types of OutOfMemoryError exceptions; demonstration program for StackOverflowError exception.

Now let's move on to the next type runtime data areas, JVM Stacks. Here are my understandings collected so far:

To validate some of these understandings and to see what will happen if the thread is having too many nested method calls. I wrote the following sample program, JvmStackCrash.java, forcing the JVM run out of memory on the JVM Stack of the default thread:

/* JvmStackCrash.java
 * Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
class JvmStackCrash {
   static long n;
   public static void main(String[] a) {
      heapCheck();
      n = 0;
      try {
         sub();
      } catch (StackOverflowError e) {
         e.printStackTrace();
         System.out.println("Maximum nested calls: "+n);
         heapCheck();
      }
   }
   private static void sub() {
      n++;
      sub();
   }
   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: ");
   }
}

Compile and run it on HotSpot 1.8:

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

C:\herong>\progra~1\java\jdk1.8.0\bin\java JvmStackCrash
Total memory: 16318464
 Free memory: 16063688
 Used memory: 254776
Press ENTER key to continue:
java.lang.StackOverflowError
	at JvmStackCrash.sub(JvmStackCrash.java:19)
	at JvmStackCrash.sub(JvmStackCrash.java:19)
	at JvmStackCrash.sub(JvmStackCrash.java:19)
...
Maximum nested calls: 7937
Total memory: 16318464
 Free memory: 15990032
 Used memory: 328432
Press ENTER key to continue:

Ok. The output tells me that the JVM Stack for the default 'main" thread can hold about 8000 frames to support about 8000 nested method calls of my sample sub() method.

Now, run it again with -Xss option:

C:\herong>\progra~1\java\jdk1.8.0\bin\java -Xss256k JvmStackCrash
Total memory: 16318464
 Free memory: 16063848
 Used memory: 254616
Press ENTER key to continue:
java.lang.StackOverflowError
	at JvmStackCrash.sub(JvmStackCrash.java:19)
...
Maximum nested calls: 6892
Total memory: 16318464
 Free memory: 15990192
 Used memory: 328272
Press ENTER key to continue:

C:\herong>\progra~1\java\jdk1.8.0\bin\java -Xss512k JvmStackCrash
Total memory: 16318464
 Free memory: 16063848
 Used memory: 254616
Press ENTER key to continue:
java.lang.StackOverflowError
	at JvmStackCrash.sub(JvmStackCrash.java:19)
...
Maximum nested calls: 14274
Total memory: 16318464
 Free memory: 15990192
 Used memory: 328272
Press ENTER key to continue:

The output confirms that we can control the JVM Stack size limit with the -Xss option. With 256 KB, the JVM Stack can hold about 7000 frames, and about 14000 frames with 512 KB.

See other parts of the book for more tutorials on JVM Stack data areas.

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

 References

 PDF Printing Version