Fix memory leak: Remove disposed Project from static maps #391
+19
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The
LanguageServerWrapper
class retained disposedProject
instances as keys in the staticprojectToLanguageServerWrapper
map, causing a memory leak as reported in Android Studio's memory profiler:When a project was closed, the
Project
reference was never removed from static maps, preventing garbage collection and causing memory to leak over time.Root Cause
Project
instances were added toprojectToLanguageServerWrapper
in the constructorLanguageServerWrapper
instances were added toprojectToLanguageWrappers
when createdprojectClosing()
listener did not:Solution
This PR introduces minimal, surgical changes to ensure proper cleanup:
1. Added
LanguageServerWrapper.dispose()
methodA dedicated cleanup method that removes the Project from
projectToLanguageServerWrapper
. This is separate fromstop()
because:stop()
is used for both temporary stops (reconnect/restart) and permanent stops (project closing)stop()
would breakforProject()
lookups after reconnectsdispose()
is only called when the project is actually being closed2. Enhanced
LSPProjectManagerListener.projectClosing()
Now properly shuts down language servers and cleans up resources:
3. Enhanced
IntellijLanguageClient.removeWrapper()
Now removes wrappers from
projectToLanguageWrappers
map and cleans up empty sets, preventing indirect retention of Project instances through wrapper references.Impact
Testing
The fix has been designed to handle both scenarios correctly:
forProject()
still worksOriginal prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.