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(runtime): clear up rootAppliedStyles #6087

Merged
merged 31 commits into from
Jan 10, 2025
Merged

Conversation

christian-bromann
Copy link
Member

@christian-bromann christian-bromann commented Dec 31, 2024

What is the current behavior?

There seems to be a memory leak within Stencil that causes performance problems as documented in:

It turns out that Stencil doesn't properly clear up its hostRefs global when elements are removed from the DOM. Given that the hostRefs global is never garbage collected, all its value will remain in memory which requires us to do some manual cleanup. Furthermore, we have been using references in another global variable called rootAppliedStyles which also never gets cleaned up.

What is the new behavior?

This patch enhances the disconnectedCallback hook to clean up detached nodes from the hostRefs and rootAppliedStyles maps.

Documentation

Does this introduce a breaking change?

  • Yes
  • No

Testing

I am investigating if we can add some memory checks through Puppeteer and Chrome Devtools.

Other information

A development release is now available at @stencil/[email protected]. If anyone is able to help validate the memory leak fix, your assistance would be greatly appreciated.

@christian-bromann
Copy link
Member Author

christian-bromann commented Jan 2, 2025

I've done some further testing on the following reproduction case: https://github.com/alicewriteswrongs/stencil-1079-memory-leak-example

Behavior before patch:
While the performance monitor showed signs that GC picks up detached DOM nodes:
Screenshot 2025-01-02 at 11 12 26 AM
It is clear that there is a minimum amount of nodes that isn't being picked up. This can be proven by logging the hostrefs which show an increasing number of entries:
Screenshot 2025-01-02 at 11 07 27 AM

Behavior after patch:
The saw curve is now much more clear to determine:

Screenshot 2025-01-02 at 11 18 56 AM

and hostrefs remain the same at all times:

Screenshot 2025-01-02 at 11 17 03 AM

@christian-bromann
Copy link
Member Author

Investigated another reproduction case: https://github.com/joewoodhouse/stencil-modal-memory-investigation

I made the same observations: the performance monitor and DOM node curve looked the same for the version with as well as without patch, however without the patch the number of nodes in the hostref WeakMap would continue to increase:

Screenshot 2025-01-02 at 11 52 01 AM

while in the patched version, it would remain around 32 nodes.

@christian-bromann christian-bromann changed the title fix(runtime): clear up detached hostRefs and rootAppliedStyles fix(runtime): clear up rootAppliedStyles Jan 7, 2025
@christian-bromann
Copy link
Member Author

Further investigations have shown that cleaning up the hostRefs Weakmap is not a good idea because we never know when or if we may re-attach a node back to the DOM again. Some of our e2e tests have shown regression in this area. I definitely believe that cleaning up the rootAppliedStyles has a positive effect and reduces the amount of detached nodes. However I can still observe nodes lingering around so my investigation will continue.

@christian-bromann christian-bromann marked this pull request as ready for review January 7, 2025 06:13
@christian-bromann christian-bromann requested a review from a team as a code owner January 7, 2025 06:13
Copy link

@gnbm gnbm left a comment

Choose a reason for hiding this comment

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

Great work and analysis @christian-bromann 🚀

@seregindev
Copy link

Hey @christian-bromann! Great work, and many thanks to you for that. Do you have a new version with the last changes that I can use for testing?

@christian-bromann
Copy link
Member Author

@seregindev I just published a new dev version @stencil/[email protected], please give it a try and let me know if you see any improvements.

@christian-bromann
Copy link
Member Author

🙈 thanks, great catch! Pushed a new dev build @stencil/[email protected]

@christian-bromann
Copy link
Member Author

Updated: @stencil/[email protected]

@christian-bromann
Copy link
Member Author

Last update: @stencil/[email protected] .. this is ready for merging now. I reviewed the fix in 3 reproduction cases:

@christian-bromann christian-bromann merged commit c4a9c9e into main Jan 10, 2025
88 checks passed
@christian-bromann christian-bromann deleted the cb/memory-leak-fix branch January 10, 2025 22:24
@christian-bromann christian-bromann restored the cb/memory-leak-fix branch January 10, 2025 22:24
@christian-bromann christian-bromann deleted the cb/memory-leak-fix branch January 10, 2025 22:24
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.

4 participants