StackOverflowError Exception Test

This section provides a tutorial example on how to produce a StackOverflowError exception by doing recursive method calls. With the default setting, Java 8 can hold about 8900 frames in a stack.

From the previous section, we understand that StackOverflowError exception occurs when a thread has too many frames in its stack and there is no room to add a new frame to make the next method call.

The StackOverflowError exception is easy to produce by doing recursive method calls. Here is an example program,, that will end with a StackOverflowError exception:

 * Copyright (c) 2014,, All Rights Reserved.
class StackOverflowTest {
   static long n;
   public static void main(String[] a) {
      n = 0;
      try {
      } catch (StackOverflowError e) {
         System.out.println("Maximum nested calls: "+n);
   private static void sub() {

Compile and run, you will get this output:


C:\>\progra~1\Java\jdk1.8.0\bin\java StackOverflowTest 2> output.txt
Maximum nested calls: 8906

C:\herong>type output.txt
   at StackOverflowTest.sub(
   at StackOverflowTest.sub(
   at StackOverflowTest.sub(
   ... (1024 repeating lines)

The output tells us that:

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