Java Runtime Data Areas
Java Virtual Machine divides managed memory into distinct data regions:
// Example of memory allocation
Object obj = new Object();
1. Program Counter Register
- Thread-private
- Points to current executing bytecode instruction
2. Java Virtual Machine Stack
- Method execution memory model
- Contains stack frames with:
- Local variable array
- Operand stack
- Return value
- Dynamic linking
3. Native Method Stack
- Handles native method calls
- Thread-private
4. Method Area
- Stores class metadata, constants, static variables
- Shared among threads
5. Heap
- Stores object instances
- Shared among threads
- Divided in to:
- Young Generation (Eden, Survivor spaces)
- Old Generation
- Permanent Generation (removed in JDK 8)
Garbage Collection
Reachability Analysis
- Mark objects reachable from GC Roots
- Unreachable objects are eligible for collection
GC Algorithms
- Mark-Sweep
- Copying
- Mark-Compact
- Generational Collection
Class Loading Mechanism
Loading Process
- Loading
- Linking (Verification, Preparation, Resolution)
- Initialization
Parent Delegation Model
- Class loaders delegate upward
- Enhances security
Class Loader Types
- Bootstrap
- Extension
- System/Application
- Custom
JVM Optimization Techniques
- Adjust heap sizes (-Xms, -Xmx)
- Tune survivor ratios (-XX:SurvivorRatio)
- Use parallel GC (-XX:+UseParallelGC)
- Consider large memory pages (-XX:+LargePageSizeInBytes)
- Monitor with tools (jps, jstack, jmap)