JVM Flashcards
What is JVM in Java?
JVM stands for Java Virtual Machine. It is the virtual machine on which Java code executes. JVM is responsible for converting bytecode into machine-specific code.
Discuss HotSpot JVM (Java Virtual Machine) Architecture in short.
HotSpot JVM consists of three main components:
Class Loader Subsystem
Responsible for loading class files.
Runtime Data Areas:
- Method Area: Stores data for each class including fields constant pool and method information.
- Heap: Where all objects are stored.
- Java thread Stacks: Each method call creates a new stack frame.
- Program Counter Registers (PC Registers): Point to the next instruction to be executed.
- Native thread stack: Contains information related to the native platform.
Execution Engine
- JIT (Just-In-Time) compiler for bytecode-to-machine code compilation
- Garbage Collector for memory management.
What is the purpose of the Class Loader Subsystem in JVM?
The Class Loader Subsystem is responsible for loading class files into memory. It ensures that classes are available for execution.
What is the Execution Engine in JVM?
The Execution Engine executes bytecode assigned to the runtime data areas. It includes the JIT compiler which compiles bytecode to machine code at runtime and the Garbage Collector for memory management.
What are the steps involved in Garbage Collection?
Garbage Collection involves three steps:1. Mark: Traverse the object graph from the root marking reachable objects as alive.2. Sweep: Delete unmarked (dead) objects.3. Compact: Defragment memory by making allocations contiguous for live objects.
What is the purpose of the Eden space in memory management during GC?
Newly created objects reside in the Eden space until it becomes full. Minor GC runs marking live objects and moving them to ‘Survivor from’ space while sweeping the Eden space.
How does a minor GC differ from a major GC?
Minor GC focuses on the young generation (Eden space and survivor spaces) while major GC traverses the entire heap memory (including tenured space).
What happens to live objects during a minor GC?
Live objects are moved from the Eden space to the survivor spaces during a minor GC.
What triggers a minor GC to run?
When the Eden space is full
How are objects moved between the survivor spaces during minor GC?
Objects are marked and moved from ‘Survivor from’ to ‘Survivor to’ space during minor GC cycles.
What is the role of the tenured space in memory management?
Tenured space holds long-lived objects that have survived multiple minor GC cycles.
How does the JVM keep track of the number of GC cycles survived by each object?
The JVM uses the -XX:MaxTenuringThreshold
parameter to track the number of GC cycles an object has survived.
What is the significance of the -XX:MaxTenuringThreshold
parameter?
It determines when objects are promoted from survivor spaces to the tenured space.
Why is frequent major GC undesirable for application performance?
Major GC causes longer pauses and can impact user interactions.
How can we detect memory issues related to the tenured space?
Frequent occurrences of ‘java.lang.OutOfMemoryError’ in logs or high CPU usage due to frequent GC runs. Can also be observed during Memory Profiling
What is the impact of memory leaks on GC behavior?
Memory leaks can fill up the tenured space leading to more frequent major GC cycles.
How does the JVM handle objects that reside in the Eden space?
Newly created objects reside in the Eden space until it becomes full.
What happens when the Eden space becomes full?
A minor GC runs moving live objects to the survivor spaces and sweeping the Eden space.
How does the JVM decide which objects to move to the survivor spaces?
Objects are selected based on their age (number of GC cycles survived).
What is the purpose of the ‘Survivor from’ and ‘Survivor to’ spaces?
They serve as intermediate spaces for objects during minor GC cycles.
How does the JVM determine when an object should be promoted to the tenured space?
Based on the configured tenuring threshold and the number of GC cycles survived.
What is the difference between a soft reference and a weak reference in Java?
Soft references allow objects to be garbage collected only when memory is low while weak references allow immediate collection.