SONARJAVA-6972 fix: clear safeSymbols to prevent memory leak - #6142
romainbrenguier wants to merge 1 commit into
Conversation
| @@ -90,6 +90,7 @@ public class BoxedBooleanExpressionsCheck extends BaseTreeVisitor implements Jav | |||
| public void scanFile(JavaFileScannerContext context) { | |||
There was a problem hiding this comment.
💡 Performance: Caches cleared only before scan, so last file's AST stays retained
The clear calls (including the new safeSymbols.clear()) run at the start of scanFile, so after the final file of the analysis is scanned, ifStatementCache still maps every ancestor Tree of each null-check usage (populated in getParentConditionalBranch, line 246) and firstNullCheckCache still holds that file's Symbols. Because those two maps are static, that data stays strongly reachable for the lifetime of the plugin classloader — it is not released when the check instance and VisitorsBridge are dropped — which keeps a whole compilation unit's tree alive in long-lived processes (SonarLint/IDE sessions), contradicting the PR description's claim that the two static caches are "properly cleared". Clearing at the end of the scan (or via the EndOfAnalysis hook, whose javadoc explicitly warns that "keeping state between files can lead to memory leaks") releases the memory instead of merely bounding it to one file.
Clear the caches after the scan in a finally block so nothing is retained once the file (and the analysis) is done.:
@Override
public void scanFile(JavaFileScannerContext context) {
this.context = context;
try {
if (context.getSemanticModel() != null) {
scan(context.getTree());
}
} finally {
ifStatementCache.clear();
firstNullCheckCache.clear();
safeSymbols.clear();
}
}
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
Code Review 👍 Approved with suggestions 0 resolved / 1 findingsFixes memory leak by clearing 💡 Performance: Caches cleared only before scan, so last file's AST stays retained📄 java-checks/src/main/java/org/sonar/java/checks/BoxedBooleanExpressionsCheck.java:81-82 📄 java-checks/src/main/java/org/sonar/java/checks/BoxedBooleanExpressionsCheck.java:90-98 📄 java-checks/src/main/java/org/sonar/java/checks/BoxedBooleanExpressionsCheck.java:225 📄 java-checks/src/main/java/org/sonar/java/checks/BoxedBooleanExpressionsCheck.java:246 The clear calls (including the new Clear the caches after the scan in a finally block so nothing is retained once the file (and the analysis) is done.🤖 Prompt for agentsOptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|




Problem
The
safeSymbolsfield inBoxedBooleanExpressionsCheckaccumulates symbols across file scans but is never cleared. This causes a memory leak when the analyzer processes multiple files, as the set continues to grow without bound.The two static caches (
ifStatementCacheandfirstNullCheckCache) are properly cleared at the start of each file scan, butsafeSymbolswas overlooked.Solution
Add
safeSymbols.clear()inscanFile()alongside the existing cache clear calls. This ensures the set is reset for each file while maintaining correct behavior within a single file.Verification