JDK, JRE and JVM
- JVM: the virtual machine that loads and runs bytecode (
.classfiles) on a specific OS/CPU. - JRE: the JVM plus the standard class libraries: everything needed to run Java.
- JDK: the JRE plus developer tools (
javac,jar,jshell,jdb).
javac compiles source to platform-independent bytecode. The JVM interprets it and its JIT compiler turns hot methods into native machine code at run time. That's "write once, run anywhere" with near-native speed.
Class loading
- Loading → Linking (verify, prepare static fields with defaults, resolve) → Initialization (run static initializers).
- Loaders: Bootstrap (core
java.*), Platform, and Application (your classpath). - Parent delegation: a loader first asks its parent, so core classes like
java.lang.Stringcan't be replaced by your own. - A class is initialized lazily, on first active use (e.g.
new, a static method call).
Runtime memory areas
| Area | Per | Holds | Error when full |
|---|---|---|---|
| Heap (young + old generation) | JVM | All objects and arrays | OutOfMemoryError: Java heap space |
| Metaspace (native memory) | JVM | Class metadata (replaced PermGen in Java 8) | OutOfMemoryError: Metaspace |
| Java stack | Thread | Frames: locals, operand stack, return address | StackOverflowError |
| PC register | Thread | Current bytecode instruction | |
| Native method stack | Thread | Frames of native (C) code |
How garbage collection works
An object is garbage when it can't be reached from any GC root (live thread stacks, static fields, JNI references). Collectors trace reachable objects from the roots and reclaim everything else, so cycles of unreachable objects are collected too.
- Generational hypothesis: most objects die young. New objects go to Eden (young generation).
- A minor GC copies surviving young objects to a survivor space; objects that survive enough collections are promoted to the old generation.
- A major / full GC cleans the old generation and is usually more expensive.
- Pauses where application threads stop are called stop-the-world.
| Collector | Good for |
|---|---|
| Serial | Small heaps, single core |
| Parallel | Throughput: batch jobs that tolerate pauses |
| G1 (default since 9) | Balanced: region-based, predictable pause goals |
| ZGC / Shenandoah | Very large heaps with millisecond-level pauses |
Java can still leak memory: objects that stay reachable but are never used again, e.g. ever-growing static maps, unremoved listeners, or caches without eviction.
Reference strength
| Type | Collected when | Use |
|---|---|---|
| Strong | Never while reachable | Normal references |
| Soft | The JVM is low on memory | Memory-sensitive caches |
| Weak | At the next GC once only weakly reachable | WeakHashMap keys, canonical maps |
| Phantom | After finalization; used with a queue | Cleanup actions (Cleaner) |
See it animated
Interview questions
Try answering out loud first, then open the model answer.
Q1Explain JDK vs JRE vs JVM.beginner
The JVM executes bytecode; the JRE is the JVM plus core libraries (what you need to run programs); the JDK is the JRE plus development tools such as the compiler. Since Java 11 a separate JRE is no longer shipped; you use the JDK or a custom runtime built with jlink.
Q2How does garbage collection decide what to free?intermediate
Reachability, not reference counting. Starting from GC roots (thread stacks, static fields, JNI handles), the collector marks every object it can reach; anything unmarked is garbage, including unreachable cycles.
Q3What are the young and old generations?intermediate
The heap is split because most objects die young. New objects are allocated in Eden; minor GCs copy survivors between survivor spaces and promote long-lived objects to the old generation, which is collected less often by major GCs.
Q4What replaced PermGen and why?advanced
Metaspace (Java 8). Class metadata moved from a fixed-size heap region to native memory that grows as needed, removing frequent OutOfMemoryError: PermGen space errors. It can still be capped with -XX:MaxMetaspaceSize.
Q5Can a Java program have a memory leak?intermediate
Yes. GC frees only unreachable objects, so anything still referenced but no longer needed leaks: growing static collections, caches without eviction, listeners never removed, ThreadLocals in thread pools, unclosed resources.
Q6What is the JIT compiler?intermediate
The just-in-time compiler watches which methods run often and compiles them from bytecode into optimized native code while the program runs, using runtime information (inlining, escape analysis). That's why Java programs speed up after warming up.
Q7What is the class loader delegation model?advanced
Before loading a class itself, a class loader delegates to its parent (Application → Platform → Bootstrap). Only if the parent can't find the class does the child load it. This keeps core classes unique and prevents them being replaced.
Quick check
0 of 4 answered