Top 10 Garbage Collection Interview Questions Every Fresher Should Know

Top 10 Garbage Collection Interview Questions Every Fresher Should Know Work through these 10 questions out loud. Open any question for the full model answer, the trap to avoid, and the follow-up a panel usually asks.

  1. What are weak references and WeakHashMap use cases?

    Attach metadata to objects without extending their lifetime — canonical mapping while object lives. When key GCs, entry drops. Do not rely on timing. For API response caching...

    Full answer

  2. Compare G1, ZGC, and Shenandoah garbage collectors.

    Default G1 fine for most Spring Boot services — tune -XX:MaxGCPauseMillis=200. Payment API p99 SLA <10ms GC pause: try -XX:+UseZGC on Java 21 large heap. Benchmark with JMH and...

    Full answer

  3. What is Metaspace and how do class loading issues cause memory leaks?

    Production set MaxMetaspaceSize 256m with monitoring. Investigate Metaspace OOM: heap dump + classloader histogram. Avoid generating unlimited dynamic proxies. OSGi/script...

    Full answer

  4. JVM container awareness and memory settings for Kubernetes?

    Container limit 1Gi: JAVA_TOOL_OPTIONS=-XX:MaxRAMPercentage=75.0 -XX:InitialRAMPercentage=50.0. Monitor container memory vs heap used in Actuator metrics. Leave headroom for...

    Full answer

  5. A REST API is slow only in production, not locally. How do you debug it?

    Enable Micrometer timers and Hibernate statistics or datasource-proxy in staging with prod-like data. Check Hikari `connections-active`, JVM GC pauses, and Feign/RestClient...

    Full answer

  6. Why is ThreadLocal problematic with virtual threads?

    Libraries storing per-request state in ThreadLocal allocate one entry per virtual thread — catastrophic memory with 100k concurrent requests. Prefer ScopedValue for request...

    Full answer

  7. Explain the happens-before relationship in the Java Memory Model.

    Thread A writes x=1 without sync; Thread B reads x may see 0 forever. volatile x or synchronized block creates happens-before. Classic DCL singleton needs volatile instance...

    Full answer

  8. What are GraalVM native image considerations for Spring Boot?

    Use spring-boot-maven-plugin native profile + GraalVM 21. Run integration tests on native binary. Register reflection for Jackson/custom serializers. Trade longer CI builds for...

    Full answer

  9. Your Spring Boot service crashes under high traffic. What could be the reasons?

    Common causes: too-small heap (`-Xmx`), Hikari pool smaller than concurrent requests, blocking calls on limited threads, missing backpressure. Fix: tune...

    Full answer

  10. Explain immutability and why String is immutable.

    Immutable objects cannot change after build — thread-safe without synchronization. String immutability allows intern pool, caching hashCode, and security when used as...

    Full answer

Garbage Collection Interview Questions and Answers · Top 50 Garbage Collection Interview Questions and Answers (2026 Edition) · Top 40 Garbage Collection Interview Questions | Must-Prepare before interview. · Garbage Collection Interview Questions Every Fresher Must Know

All interview question lists · Interview guides · Home · Support the platform