JVM Tutorials - Herong's Tutorial Examples - Version 4.23, by Dr. Herong Yang
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, StackOverflowTest.java, that will end with a StackOverflowError exception:
/* StackOverflowTest.java * Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ class StackOverflowTest { static long n; public static void main(String[] a) { n = 0; try { sub(); } catch (StackOverflowError e) { e.printStackTrace(); System.out.println("Maximum nested calls: "+n); } } private static void sub() { n++; sub(); } }
Compile and run StackOverflowTest.java, you will get this output:
C:\herong>\progra~1\Java\jdk1.8.0\bin\javac StackOverflowTest.java C:\>\progra~1\Java\jdk1.8.0\bin\java StackOverflowTest 2> output.txt Maximum nested calls: 8906 C:\herong>type output.txt java.lang.StackOverflowError at StackOverflowTest.sub(StackOverflowTest.java:18) at StackOverflowTest.sub(StackOverflowTest.java:18) at StackOverflowTest.sub(StackOverflowTest.java:18) ... (1024 repeating lines)
The output tells us that:
Last update: 2014.
Table of Contents
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
JRockit JVM 28.2.7 by Oracle Corporation
Memory Management and Garbage Collectors
►JVM Stack, Frame and Stack Overflow
►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
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