JVM Stack Expansion and Footprint

This section provides a tutorial example on how to test the impact of stack expansion on JVM footprint. Stacks are dynamically expanding and limited by the -Xss option.

In this tutorial, we are going to look at the impact of stack size on JVM footprint, using this example program, MultiStackThread.java:

/* MultiStackThread.java
 * Copyright (c) 2014, HerongYang.com, All Rights Reserved.
class MultiStackThread extends Thread {
   public int maxCount;
   private int curCount;
   public static void main(String[] arg) {
      MultiStackThread t;
      int m = 4;
      int f = 1000;
      if (arg.length>0) m = Integer.parseInt(arg[0]);
      if (arg.length>1) f = Integer.parseInt(arg[1]);
      for (int n=0; n<m; n++) {
         t = new MultiStackThread();
         t.maxCount = f;
      System.out.println("Number of threads started: "+m);
      Runtime rt = Runtime.getRuntime();
      long dt0 = System.currentTimeMillis()/1000;
      while (true) {
         try {
            long tm = rt.totalMemory();
            long fm = rt.freeMemory();
            long dt = System.currentTimeMillis()/1000 - dt0;
            System.out.println("Time, heap total, free and used: "
               +dt+", "+tm+", "+fm+", "+(tm-fm));
         } catch (InterruptedException e) {
   public void run() {
      curCount = 0;
      try {Thread.sleep(1000*60*60);} 
      catch (InterruptedException e) {}
   private void sub() {
      try {      
         if (curCount<maxCount) sub();
         else System.out.println("Count limit "+curCount
            +" reached for thread: "+this.getId());
      } catch (StackOverflowError e) {
         System.out.println("Stack overflow at "+curCount
            +" for thread: "+this.getId());

2 parameters are used to control the execution of MultiStackThread.java:

First, let's compile and run the program with 1 thread, 0 nested method call, and 1 MB as the stack limit:

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

C:\>\progra~1\Java\jdk1.8.0\bin\java -Xss1m MultiStackThread 1 0
Number of threads started: 1
Count limit 1 reached for thread: 8
Time, heap total, free and used: 1, 16252928, 15616224, 636704
Time, heap total, free and used: 2, 16252928, 15616224, 636704
Time, heap total, free and used: 3, 16252928, 15616224, 636704

While the program is running in sleep mode, run the tasklist command in another command window:

Image Name         PID Session Name        Session#    Mem Usage
============= ======== ================ =========== ============
java.exe          9432 Console                    1     14,020 K

So the program works. The JVM footprint (Mem. Usage) is 14,020 KB with 1 thread and an almost empty stack (0 nested call).

Now let's rerun the program with different numbers of nested method calls and see the impact on the JVM footprint (memory usage). Here is the result from my Windows system:

Frames    Mem Usage   (-Xss1m and 1 thread)
     0     14,020 K
    10     14,128 K
   100     14,056 K
  1000     14,100 K
  5000     14,288 K
 10000     14,528 K
 15000     14,784 K
 16000     15,008 K
 17000     15,068 K
 18000     15,028 K
 18429     15,032 K (Stack overflow)


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

 Garbage Collection Tests

JVM Stack, Frame and Stack Overflow

 What Is JVM Stack?

 StackOverflowError Exception Test

 -Xss JVM Option for Stack Size

 Frame Impact of Extra Statements

JVM Stack Expansion and Footprint

 JVM Stack Expansion and OutOfMemoryError

 Largest Stack Size for HotSpot on Windows

 Default Stack Sizes of HotSpot and JRockit

 JRockit Frame Size Smaller than HotSpot

 JRockit Expanding Stacks in Bigger Chunks

 JRockit Running Out Of Memory Quicker

 Largest Stack Size for JRockit on Windows

 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