Java
Why obsolete references should be eliminated in Java?
Why unused object references can cause memory leaks, and where to clear them.
2 views
- Obsolete references prevent garbage collection → cause memory leaks
- Java GC reclaims only unreachable objects
- Common in self-managed memory structures (stacks, queues, arrays)
- Classic bug: popped elements stay referenced in the backing array
- Result: heap grows over time even after logical removal
- Fix: explicitly set array slots / collection entries to null after removal
- Nulling removes the last live reference → makes object eligible for GC
- Most effective in long-lived containers where elements are removed
- Also critical in caches, listener lists, callback registrations
- Best prevention: keep variable scopes narrow
- Let variables fall out of scope naturally whenever possible
- Only null out when you manually manage memory (e.g., array-based structures)
- Avoid over-nulling — it clutters code when not needed
- This is one of the most common sources of insidious memory leaks in Java