Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak issue with dev tools #1335

Merged
merged 2 commits into from Apr 25, 2024
Merged

Conversation

hehaijin
Copy link
Contributor

@hehaijin hehaijin commented Apr 20, 2024

This is to fix the memory leak identified in #1332

Each copy of client data to extension is wrapped inside a promise,
and this data is supposed to be removed and GCed but right now it's not.

in src/extension/messageAdapters.ts createPortMessageAdapter
the line port.onDisconnect.addListener creates
a closure that holds the reference to handler passed in, and only release when disconnect,
which may not happen very soon.
And in src/extension/rpc.ts createRpcClient function, the the handler holds
a reference to the promise that get data from client side.
so the promise data never got garbage collected.

Removed this event handler from onDisconnect to fix the memory leak issue.

May need to address how to properly remove it in onDisconnect, but it's lower priority.
closes #1332

@hehaijin hehaijin requested a review from a team as a code owner April 20, 2024 14:10
@apollo-cla
Copy link

@hehaijin: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@hehaijin hehaijin force-pushed the fix-leak branch 2 times, most recently from 6612b00 to 75942a8 Compare April 20, 2024 14:29
@@ -18,9 +18,6 @@ export function createPortMessageAdapter<
return {
addListener(listener) {
port.onMessage.addListener(listener);
port.onDisconnect.addListener(() => {
Copy link
Contributor Author

@hehaijin hehaijin Apr 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This event handler would hold reference to listener and prevent it from garbage collected.

@hehaijin hehaijin force-pushed the fix-leak branch 2 times, most recently from 75942a8 to 3f0b6b0 Compare April 20, 2024 22:42
@jerelmiller
Copy link
Member

Hey @hehaijin 👋

Thanks so much for this PR! I don't actually remember why I added the code to remove the onMessage event listener in onDisconnect. Googling around, I can't seem to find anything that specifies that this is needed. Perhaps I saw it somewhere and blindly copied it 😅.

In any case, this PR looks good and I think its a small enough change that should be fine. If you wouldn't mind adding a changeset to this PR, I'd be happy to get this merged and released. Appreciate all the digging you've been doing into the memory consumption of the devtools! We'd welcome any improvements you find!

@hehaijin
Copy link
Contributor Author

Thanks @jerelmiller for reviewing. Added changeset.

@hehaijin
Copy link
Contributor Author

@jerelmiller I have some thoughts regarding performance posted in discussion: #1337
it would be nice to know what your guys think.

Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Thanks again so much for this PR!

Copy link

relativeci bot commented Apr 25, 2024

#214 Bundle Size — 851.54KiB (-0.02%).

89f7a05(current) vs 10e61ed main#212(baseline)

Warning

Bundle contains 5 duplicate packages – View duplicate packages

Bundle metrics  Change 3 changes Improvement 1 improvement
                 Current
#214
     Baseline
#212
Improvement  Initial JS 814.14KiB(-0.02%) 814.34KiB
No change  Initial CSS 0B 0B
Change  Cache Invalidation 8.39% 0%
No change  Chunks 5 5
No change  Assets 12 12
No change  Modules 638 638
No change  Duplicate Modules 35 35
Change  Duplicate Code 5.2%(-0.38%) 5.22%
No change  Packages 58 58
No change  Duplicate Packages 4 4
Bundle size by type  Change 1 change Improvement 1 improvement
                 Current
#214
     Baseline
#212
Improvement  JS 814.14KiB (-0.02%) 814.34KiB
No change  IMG 35.85KiB 35.85KiB
No change  HTML 810B 810B
No change  Other 778B 778B

Bundle analysis reportBranch hehaijin:fix-leakProject dashboard

@jerelmiller jerelmiller merged commit 62e8bd6 into apollographql:main Apr 25, 2024
9 checks passed
@github-actions github-actions bot mentioned this pull request Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Serious memory leak issue with the devtools extension
3 participants