GamifiedGamified challengesPracticeRaceLeaderboardPricing

Learn data structures by watching them move, then doing it yourself. Press M anytime to toggle sound.

A product of Amypo Technologies, Coimbatore.

Learn

  • All topics
  • Learning path
  • How it works
  • About dotprep

Practice

  • Practice arena
  • Daily challenge
  • Gamified challenges
  • Race mode
  • Leaderboard

Interview prep

  • Java interview
  • Coding questions
  • Company questions
  • System design
  • Jobs & careers

You

  • Dashboard
  • Settings
  • Pricing
  • Create account

© 2026 dotprep

Content last updated 20 November 2026

GamifiedGamified challengesPracticeRaceLeaderboardPricing
Java interviewJVM internals & garbage collection

JVM internals & garbage collection

JDK vs JRE vs JVM, class loading, memory areas, how garbage collection works and the collectors you can choose.

advanced 16 min read 7 interview questions

On this page

1/7
  1. 01JDK, JRE and JVM
  2. 02Class loading
  3. 03Runtime memory areas
  4. 04How garbage collection works
  5. 05Reference strength
  6. 06Interview questions
  7. 07Quick check
Back to top

JDK, JRE and JVM

  • JVM: the virtual machine that loads and runs bytecode (.class files) 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.String can'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

AreaPerHoldsError when full
Heap (young + old generation)JVMAll objects and arraysOutOfMemoryError: Java heap space
Metaspace (native memory)JVMClass metadata (replaced PermGen in Java 8)OutOfMemoryError: Metaspace
Java stackThreadFrames: locals, operand stack, return addressStackOverflowError
PC registerThreadCurrent bytecode instruction
Native method stackThreadFrames 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.
CollectorGood for
SerialSmall heaps, single core
ParallelThroughput: batch jobs that tolerate pauses
G1 (default since 9)Balanced: region-based, predictable pause goals
ZGC / ShenandoahVery 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

TypeCollected whenUse
StrongNever while reachableNormal references
SoftThe JVM is low on memoryMemory-sensitive caches
WeakAt the next GC once only weakly reachableWeakHashMap keys, canonical maps
PhantomAfter finalization; used with a queueCleanup actions (Cleaner)

See it animated

Stack vs heap memory

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

Question 1

1.Where are objects allocated first?

Question 2

2.Infinite recursion causes…

Question 3

3.Which reference is cleared first when memory runs low but before an OOM?

Question 4

4.The default collector since Java 9 is…

0 of 4 answered

Multithreading & concurrencyDesign patterns & SOLID
Log inStart free