JVM Tutorials - Herong's Tutorial Examples
∟JVM Runtime Data Areas
This chapter provides tutorial notes and example codes on JVM runtime data areas. Topics include introduction of JVM runtime data areas: Method Area, Heap, Direct Memory, PC Registers, JVM Stacks, and Native Method Stacks; different between thread-shared and per-thread data areas; demonstration programs for different types of OutOfMemoryError exceptions; demonstration program for StackOverflowError exception.
These sections are omitted from this Web preview version. To view the full content,
see information on how to obtain the full version this book.
What Are Runtime Data Areas
What Is Method Area or Metaspace
"-XX:MaxMetaspaceSize=..." and "-Xlog:gc*"
Loading EmptyClass Costs 130K in "class space"
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
What Is Heap Area
Heap Expansion Demonstration
allocateMemory() Method on Direct Memory
JVM Stack Expansion Demonstration
PC Register and Native Method Stack
Takeaways:
- JVM uses 6 types of data areas at runtime: Method Area, Heap, Direct Memory, PC Registers, JVM Stacks, and Native Method Stacks.
- Method Area, also called Permanent Generation (PermGen) space or Class Metaspace, stores class definitions and constants.
- Heap stores dynamically created data objects (instances of classes and arrays).
- Direct Memory stores dynamically created ByteBuffer objects and other data elements created by native methods.
- Method Area, Heap and Direct Memory are created at startup time and shared by all threads.
- Method Area, Heap are managed by garbage collectors to remove unused objects.
- "-XX:MaxMetaspaceSize=..." option sets the size limit on the Method Area.
- "-Xmx" option sets the size limit on Heap.
- When the Method Area runs out of memory, JVM throws the "OutOfMemoryError: Metaspace" exception.
- When the Heap runs out of memory, JVM throws the "OutOfMemoryError: Java heap space" exception.
- When the Direct Memory runs out of memory, JVM throws the "OutOfMemoryError: Direct buffer memory" exception.
- PC Registers, JVM Stacks and Native Method Stacks are created when threads are created and associated with threads one copy per thread.
- A PC Register, short for Program Counter Register, stores the address of the instruction currently being executed in the thread the PC Registered is associated to.
- A JVM Stack stores a stack of frames. The top frame stores local variables, partial calculation results, method call information
for the method currently being executed in the thread the JVM Stack is associated to.
- A Native Method Stack stores a stack of frames similar to a JVM Stack. But it is used to help executing native methods
in the thread the JVM Stack is associated to.
- "-Xss" option sets the size limit on a single JVM Stack data area.
- When a JVM Stack runs out of memory, JVM throws the "StackOverflowError" exception.
Table of Contents
About This Book
JVM (Java Virtual Machine) Specification
Java HotSpot VM - JVM by Oracle/Sun
java.lang.Runtime Class - The JVM Instance
java.lang.System Class - The Operating System
ClassLoader Class - Class Loaders
Class Class - Class Reflections
►JVM Runtime Data Areas
JVM Stack, Frame and Stack Overflow
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
OpenJ9 by Eclipse Foundation
JRockit JVM 28.2.7 by Oracle Corporation
Archived Tutorials
References
Full Version in PDF/EPUB