Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Java
Javahard

How does Java GC work at a high level (and why is it generational)?

Tags
#gc#jvm#memory
Back to categoryPractice quiz

Answer

The heap is typically split into young/old generations: most objects die young, so collecting the young gen is cheap and frequent (minor GC). Surviving objects are promoted; old gen collections are rarer and usually more expensive.

Advanced answer

Deep dive

At a high level, GC:

  • Finds reachable objects (mark).
  • Reclaims memory of unreachable objects (sweep) and may compact to reduce fragmentation.

Generational GC is based on the “generational hypothesis”: most objects die young. So the heap is split:

  • Young gen (Eden + survivor spaces): collected often (minor GC) and usually fast.
  • Old gen: collected less often (major/mixed collections) and typically more expensive.

Practical view

  • Allocation is cheap; GC cost appears as pauses and CPU overhead.
  • Modern collectors (G1, ZGC, Shenandoah) trade throughput vs pause-time differently.

Common pitfalls

  • Thinking GC frees objects “when they go out of scope” (it’s reachability-based).
  • Creating lots of short-lived garbage in hot paths.

Related questions

Java
JIT compilation: what is it and why do Java apps “warm up”?
#java#jit#performance
Java
Generational garbage collection: why does the JVM split memory into young/old?
#java#gc#jvm
Java
ClassLoaders: what are they and why can they cause surprising ClassCastException?
#java
  • Holding references accidentally (memory leaks via caches, listeners, static maps).
  • #classloader
    #jvm
    Java
    What is the Java classpath and what is it used for?
    #classpath#jvm#jar
    Java
    JDK vs JRE vs JVM - what is each?
    #jvm#jre#jdk
    Java
    WeakReference — what is it and when is it useful?
    #weakreference#weakhashmap#gc