Do Not Invoke the Garbage Collector Explicitly

ID

java.explicit_gc_call

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

best-practice, reliability

Description

Reports explicit calls to the garbage collector via System.gc() or Runtime.getRuntime().gc().

Rationale

Manually invoking the garbage collector is almost never necessary and can significantly degrade application performance. The JVM’s garbage collector is designed to manage memory automatically and is much better at determining when collection is needed. Explicit gc() calls cause stop-the-world pauses at unpredictable times, disrupting throughput and latency.

// Bad - manual garbage collection
System.gc();
Runtime.getRuntime().gc();

Remediation

Remove explicit garbage collector calls and let the JVM manage memory automatically. If memory pressure is a concern, investigate object allocation patterns, reduce garbage generation, or tune GC parameters via JVM flags.

// Good - no explicit gc calls, let JVM manage memory
public void process() {
    // ... application logic ...
}

References