Runtime totalMemory/freeMemory In Code
ID |
java.runtime_totalmemory_in_code |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Java |
Tags |
best-practice, code-style |
Description
Reports calls to Runtime.totalMemory() and Runtime.freeMemory(). These methods are commonly associated with manual debugging or profiling activities and should not remain in production code.
Rationale
Inline memory checks using Runtime.totalMemory() or Runtime.freeMemory() are typically leftover debugging code. They provide point-in-time snapshots that are unreliable for monitoring and add unnecessary overhead:
public void process() {
System.out.println("Memory: " + Runtime.getRuntime().totalMemory());
doWork();
System.out.println("Free: " + Runtime.getRuntime().freeMemory());
}