Skip to content

Memory leak on component unmount #12102

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

Closed
markpsiano opened this issue Jun 19, 2024 · 3 comments
Closed

Memory leak on component unmount #12102

markpsiano opened this issue Jun 19, 2024 · 3 comments

Comments

@markpsiano
Copy link
Contributor

markpsiano commented Jun 19, 2024

Describe the bug

When a component is unmounted, event listeners which were attached to document on component mount are not removed. This leads to a memory leak as there are unused event listeners leftover after components unmount.

This can be verified by using chrome devtools, and taking heap snapshots at various points throughout page navigation/components mounting and unmounting, where the number of event listeners increases over time. It can also be verified in elements -> event listeners tab in devtools, where there are an increasing number of listeners upon mounting and unmounting components.

Fix is here: #12101

I'm not sure if this is the correct way of reporting an issue (making a fix as well as issue ticket), but just wanted more visibility here. If not, feel free to close this one.

Reproduction

Call unmount() on a component, observe how document event listeners are still retained, number of event listeners in memory using chrome memory devtools will show increasing amount as components are unmounted

Logs

No response

System Info

System:
    OS: macOS 12.2.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 75.61 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 20.13.1 - /usr/local/bin/node
    npm: 10.5.2 - /usr/local/bin/npm
  Browsers:
    Chrome: 126.0.6478.62
    Safari: 15.3
  npmPackages:
    rollup: ^4.18.0 => 4.18.0 
    svelte: 5.0.0-next.155 => 5.0.0-next.155

Severity

annoyance

@trueadm
Copy link
Contributor

trueadm commented Jun 20, 2024

Closed with #12105. Thanks also for putting up a fix! Sorry I missed it originally – it's quite rare for someone to put up an issue and link to the fix at the same time :)

@mcullifer
Copy link

mcullifer commented Mar 11, 2025

I am still experiencing this issue. Despite the element unmounting (navigating to a new page) it still seems to hold memory of an object used in an onclick function. In my case it's a very very large object so this is problematic. The onclick prop of my Button is from svelte/elements HTMLButtonAttributes rest props. The component has an optional prop for performing actions on close which is why this button does onClose?.(image). It also has the CSSTransition thing which mean it could be related to #15203 . Not sure what to do to get around this.

Image

Image

Image

@trueadm
Copy link
Contributor

trueadm commented Mar 11, 2025

It's almost impossible to fix this without some form of reproduction.

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

No branches or pull requests

3 participants