Java / Java 17 Garbage Collection Interview Questions
Why should you avoid explicit calls to System.gc()?
System.gc() is only a request, not a command - the JVM is free to ignore it - but by default most collectors treat it as a signal to run a full, stop-the-world garbage collection, which is the most expensive kind of collection available.
Calling it from application code removes control from the collector's own heuristics, which are usually better tuned to actual allocation patterns than a manually chosen call site, and can introduce large, unpredictable latency spikes exactly when you didn't plan for one - especially damaging for latency-sensitive services running on otherwise low-pause collectors like G1, ZGC, or Shenandoah.
If a library or framework is calling it unexpectedly, -XX:+DisableExplicitGC makes the JVM ignore such calls entirely as a mitigation, though the better long-term fix is removing the call at the source.
More Related questions...