JVM Tutorials - Herong's Tutorial Examples - Version 4.23, by Dr. Herong Yang
OutOfMemoryError on the Method Area
This section provides a tutorial example on how to measure the JVM footprint impact on the Method Area expansion and condition of the OutOfMemoryError exception.
The first run of MethodAreaLoadJar.java is on HotSpot 1.8 with default memory configuration:
C:\herong>\progra~1\java\jdk1.8.0\bin\javac MethodAreaLoadJar.java C:\herong>\progra~1\java\jdk1.8.0\bin\java MethodAreaLoadJar Total memory: 16252928 Free memory: 15709328 Used memory: 543600 Press ENTER key to continue:
Before pressing ENTER key, open another command window and check the JVM footprint and rt.jar file size:
C:\herong>dir \progra~1\java\jdk1.8.0\jre\lib\rt.jar 62,653,574 rt.jar C:\herong>tasklist Image Name PID Session Name Session# Mem Usage ========== ======== ============ =========== ============ java.exe 8324 Console 1 14,720 K
Now press ENTER key to load all classes and check the footprint again:
Loading all classes in: C:\progra~1\java\jdk1.8.0\jre\lib\rt.jar # of classes loaded: 19575 Total memory: 16318464 Free memory: 5301240 Used memory: 11017224 Press ENTER key to continue: (in the other window) C:\herong>tasklist java.exe 8324 Console 1 88,404 K
Then press ENTER key to run gc() and check the footprint again:
Total memory: 16318464 Free memory: 9614984 Used memory: 6703480 Press ENTER key to continue: (in the other window) C:\herong>tasklist java.exe 8324 Console 1 88,828 K
The test output tells us that:
The next run of MethodAreaLoadJar.java is on HotSpot 1.8 with a lower Method Area limit to produce an out of memory condition:
C:\herong>\progra~1\java\jdk1.8.0\bin\java -Xms5m -Xmx7m -XX:MetaspaceSize=20m -XX:MaxMetaspaceSize=30m MethodAreaLoadJar Total memory: 6094848 Free memory: 5637912 Used memory: 456936 Press ENTER key to continue: Loading all classes in: C:\progra~1\java\jdk1.8.0\jre\lib\rt.jar Exception in thread "main" java.lang.OutOfMemoryError: Metaspace at java.lang.ClassLoader.findBootstrapClass(Native Method) at java.lang.ClassLoader.findBootstrapClassOrNull(ClassLoader.ja... at java.lang.ClassLoader.loadClass(ClassLoader.java:413) at java.lang.ClassLoader.loadClass(ClassLoader.java:411) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at MethodAreaLoadJar.loadRtJar(MethodAreaLoadJar.java:31) at MethodAreaLoadJar.main(MethodAreaLoadJar.java:10)
Cool. OutOfMemoryError exception on the "Metaspac", the Method Area, happens as expected.
Of course, we can slowly increase the Method Area limit to find out how big the Method Area needs to be to hold all classes in the rt.jar. The answer is 42 MB.
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
Method Area Expansion Demonstration
►OutOfMemoryError on the Method Area
Method Area Growth with Dynamically Generated Classes
Garbage Collection Issue with Dynamically Generated Classes
Interned Strings Stored in Heap
Direct Memory Expansion Demonstration
allocateMemory() Method on Direct Memory
JVM Stack Expansion Demonstration
PC Register and Native Method Stack
Memory Management and Garbage Collectors
JVM Stack, Frame and Stack Overflow
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