Garbage Collection Interview Questions with Answers for Freshers
Garbage Collection Interview Questions with Answers for Freshers Work through these 12 questions out loud. Open any question for the full model answer, the trap to avoid, and the follow-up a panel usually asks.
Explain garbage collection basics in the JVM.
New objects allocate in Eden; minor GC copies survivors to survivor spaces, then old gen. When old gen fills, major/full GC runs — longer pauses. Tune -Xmx, choose G1 for...
How do CompletableFuture and virtual threads compare for concurrency?
Use virtual threads when business logic is naturally blocking — call DB, call HTTP, return. Use CompletableFuture when composing multiple independent async results with...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
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