JVM Tutorials - Herong's Tutorial Examples - Version 4.23, by Dr. Herong Yang
JVM Stack Expansion and OutOfMemoryError
This section provides a tutorial example on how to test JVM footprint increases when more threads are running requiring more stacks. OutOfMemoryError occurs when Windows 32-bit system stops providing more than 1.6 GB memory.
Now let's continue our test on MultiStackThread.java with 4 threads and see the impact on the JVM footprint:
(-Xss1m) 1 Thread 4 Threads Frames Mem Usage Mem Usage 0 14,020 K 14,220 K 10 14,128 K 14,084 K 100 14,056 K 14,192 K 1000 14,100 K 14,412 K 5000 14,288 K 15,292 K 10000 14,528 K 16,532 K 15000 14,784 K 17,668 K 16000 15,008 K 18,248 K (Stack overflow) 17000 15,068 K 18000 15,028 K 18429 15,032 K (Stack overflow)
The result tells us that:
Here is the result with 16 threads. The memory usage increases as expected:
(-Xss1m) 1 Thread 4 Threads 16 Threads Frames Mem Usage Mem Usage Mem Usage 0 14,020 K 14,220 K 14,700 K 10 14,128 K 14,084 K 14,888 K 100 14,056 K 14,192 K 14,804 K 1000 14,100 K 14,412 K 16,000 K 5000 14,288 K 15,292 K 19,732 K 10000 14,528 K 16,532 K 24,768 K 15000 14,784 K 17,668 K 29,640 K 16000 15,008 K 18,248 K 33,352 K (Stack overflow) 17000 15,068 K 18000 15,028 K 19000 15,032 K (Stack overflow)
If we keep increase the number of threads, the JVM should run out of memory on the Windows system. Right? Let's try it:
Threads Mem Usage (-Xss1m, 16000 frames) 1 15,008 K No error 4 18,248 K StackOverflow 16 33,352 K StackOverflow 32 51,336 K StackOverflow 64 84,380 K StackOverflow 128 150,264 K StackOverflow 1024 1,072,008 K StackOverflow 2048 OutOfMemoryError
When I ran 2048 threads with 16000 frames, many threads reached the StackOverflow exception point and the JVM memory usage reached 1,347,520 K. Then the JVM hang after generating the following exception:
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Thread.java:713) at MultiStackThread.main(MultiStackThread.java:16)
The exception happened at MultiStackThread.java:16, which the "t.start();" statement. So the program failed to launch all 2048 threads.
When I tried to break the execution with <Ctrl-C>, I got the following error:
Java HotSpot(TM) Client VM warning: Exception java.lang.OutOfMemoryError occurred dispatching signal UNKNOWN to handler- the VM may need to be forcibly terminated
I had to use Task Manager to kill the JVM.
The OutOfMemoryError exception is caused by the Windows 32-bit system refusing to provide more than 1.6GB of memory to any running processes.
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