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 detached hostRefs and rootAppliedStyles #6087

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

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.

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.

1 participant