JVM Tutorials - Herong's Tutorial Examples - Version 4.23, by Dr. Herong Yang
Frame Impact of Extra Statements
This section provides a tutorial example on how to compare the frame size of two similar recursive call methods in two threads. Extra 16 bytes are needed in the frame for an extra statement in the method.
From the previous tutorial, we learned that the frame size for calling a pretty empty method is 32 bytes. In this tutorial, we will try to see the impact on the frame size when additional statements are added to the method.
Here is the first thread, StackThread1.java, that contains an almost empty recursive method:
/* StackThread1.java * Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ import java.util.*; class StackThread1 extends Thread { public long n; public void run() { try { sub(); } catch (StackOverflowError e) { System.out.println("Maximum calls in Thread 1: "+n +" at "+System.currentTimeMillis()); } } private void sub() { n++; sub(); } }
Here is the second thread, StackThread2.java, that contains a similar recursive method with one extra statement:
/* StackThread2.java * Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ import java.util.*; class StackThread2 extends Thread { public long n; public double x; public void run() { try { sub(); } catch (StackOverflowError e) { System.out.println("Maximum calls in Thread 2: "+n +" at "+System.currentTimeMillis()); } } private void sub() { n++; x = Math.sin(System.currentTimeMillis()); sub(); } }
The following program will run both threads as the same time to compare how many frame each thread can create:
/* StackFrameSizeTest.java * Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ class StackFrameSizeTest { public static void main(String[] a) { StackThread1 t1 = new StackThread1(); t1.start(); StackThread2 t2 = new StackThread2(); t2.start(); } }
The first test run shows that everything is working as expected:
C:\herong>\progra~1\Java\jdk1.8.0\bin\javac StackFrameSizeTest.java C:\>\progra~1\Java\jdk1.8.0\bin\java -Xss128k StackFrameSizeTest Maximum calls in Thread 1: 2558 at 1397254338535 Maximum calls in Thread 2: 2215 at 1397254338540
Comparing with the first thread, the second thread:
With more tests using different -Xss values, we should be able estimate the frame size of the sub() in both threads:
-Xss Thread 1 Thread 2 Thread 1 Thread 2 Stack Frames Frames Frame Frame Size Size Size 128k 2558 2215 51.24 59.17 256k 5755 4984 45.55 52.60 512k 14862 10408 35.28 50.37 1m 30419 21339 34.47 49.14 64m 2094643 1397600 32.04 48.02 128m 4191644 2795731 32.02 48.01
Clearly, the frame size of thread 2 is 16 bytes larger than thread 1. This means that the "x = Math.sin(System.currentTimeMillis());" statement requires extra 16 bytes in the current frame.
This also tells us that frames created for different methods have different sizes.
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