Explicit GC Call
ID |
kotlin.explicit_gc_call |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Kotlin |
Tags |
best-practice, performance |
Description
Reports explicit calls to System.gc() or Runtime.getRuntime().gc(). Invoking
the garbage collector from application code disrupts the JVM’s autonomous scheduling
and almost always produces the opposite of the intended effect.
Rationale
Modern JVM garbage collectors (G1, ZGC, Shenandoah) are highly adaptive. They
schedule collection based on heap pressure, allocation rates, and pause-time goals.
An explicit System.gc() call can trigger a full GC at the worst possible moment,
causing a significant pause, inflating latency measurements, and interfering with
the collector’s internal heuristics. In some JVM configurations the call is silently
ignored anyway.
// Bad
fun cleanup() {
System.gc()
Runtime.getRuntime().gc()
}
// Good — tune the JVM instead
// -XX:+UseG1GC -Xmx2g