Skip to content

fix: remove memory leak from retaining old DOM elements#11197

Merged
trueadm merged 4 commits into
mainfrom
fix-memory-leak-3
Apr 17, 2024
Merged

fix: remove memory leak from retaining old DOM elements#11197
trueadm merged 4 commits into
mainfrom
fix-memory-leak-3

Conversation

@trueadm

@trueadm trueadm commented Apr 16, 2024

Copy link
Copy Markdown
Contributor

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!

@changeset-bot

changeset-bot Bot commented Apr 16, 2024

Copy link
Copy Markdown

🦋 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

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