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: remove memory leak from retaining old DOM elements #11197

Merged
merged 4 commits into from
Apr 17, 2024
Merged

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Apr 16, 2024

This was an interesting one to fix, happy to take feedback on tweaking the implementation.

Today, we have a common memory leak retaining DOM elements with fragment templates. The block effect has dom property with is just the [...fragment.childNodes] and that might seem innocent, but it's actually quite the opposite. That's because the template fragment we clone is populated as render effects and other logic blocks consequently add content to the fragment as we go (each blocks, if blocks etc). If that content gets cleared, it's never removed from the dom array and is stored forever.

We don't need to do this though. Instead we can capture the effect dom at the start of the template and only push into template nodes which we no belong only to that given effect. That means we stop pushing in DOM elements from other blocks – with the exception of HTML and dynamic elements – which don't get their own template.

I also encountered a bug in the existing system with dynamic HTML blocks in each blocks. This PR also fixes that too!

Copy link

changeset-bot bot commented Apr 16, 2024

🦋 Changeset detected

Latest commit: c2dfda1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@trueadm trueadm merged commit 777527b into main Apr 17, 2024
8 checks passed
@trueadm trueadm deleted the fix-memory-leak-3 branch April 17, 2024 09:02
trueadm pushed a commit that referenced this pull request Apr 18, 2024
* simplify

* make each item first nodes explicit

* remove a couple of var declarations
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.

None yet

1 participant