You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: Deleting Element with @State causes memory leak when used in componentDidLoad lifecycle hook #3158
I have searched for existing issues that already report this problem, without success.
Stencil Version
2.11.0
Current Behavior
When a @State variable is changed in the componentDidLoad lifecycle hook and that component is detached (removed) from the DOM, it remains in the Javascript memory, causing a memory leak.
Expected Behavior
The component ShadowRoot, nodes, event listeners etc. are not kept in the memory when the component is detached.
Steps to Reproduce
Add a @State variable
Change the value of the state variable in the componentDidLoad() hook
Remove the element from the DOM
Run a profile in the Chrome Dev Tools Memory tab (click the Collect garbage button beforehand for cleaner result)
You should see detached HTMLElement, ShadowRoot, StyleElement etc.
Thanks for this! I suspect that because the element is being removed outside of Stencil, it doesn't know that it's been removed, which is leading to the references there in the screenshot. I'm going to mark the issue to get ingested for us to take a closer look at.
changed the title [-]bug: @State causes memory leak when used in componentDidLoad lifecycle hook[/-][+]bug: Deleting Element with @State causes memory leak when used in componentDidLoad lifecycle hook[/+]on Dec 3, 2021
@tarqwara hello there! I know it's been quite a while since you opened this issue, but I wanted to ask if you were seeing this error outside of running with the dev server?
I'm investigating #3607 today and was looking at this issue as possibly related. After upgrading your reproduction case to @stencil/[email protected] I was able to reproduce the issue if I used the Stencil dev server (stencil build --dev --watch --serve) but if I instead run a full build and then serve the results, doing something like:
npx http-serve www
I no longer see the issue (meaning, after I 'detach' the element I don't see any DetachedHTMLElement objects in a memory snapshot). This makes me suspect that something in the Stencil dev server code is holding on to a reference to those elements.
If you're still up for it it would be great if you could confirm under what circumstances you've run into the error!
Hey thanks for providing that repro! I did notice that the relevant objects are being logged to the console, which I believe will prevent them from getting garbage collected (see this article for an explanation of that). Would it be possible for you to test if it's reproducible without the logging?
Hmm, looks like you are right. It happens only in the dev server and not in the final build output.
So does it mean that the issue #3607 also happens only in dev server.
Also I am trying to log the hostRefs value without it being logged with my sample but looks like both dev server and production builds work fine, without the memory leak in the simple sample.
I think #3607 is a separate issue I think, although unfortunately I still haven't had time to fully dig into it, so I don't think it is dev server specific. It's a complicated one! Garbage collection is supposed to make our lives easy, not harder!
Activity
rwaskiewicz commentedon Dec 3, 2021
Thanks for this! I suspect that because the element is being removed outside of Stencil, it doesn't know that it's been removed, which is leading to the references there in the screenshot. I'm going to mark the issue to get ingested for us to take a closer look at.
[-]bug: @State causes memory leak when used in componentDidLoad lifecycle hook[/-][+]bug: Deleting Element with @State causes memory leak when used in componentDidLoad lifecycle hook[/+]danielalvenstrand commentedon May 2, 2022
Hi, have you had the time to take a deeper look into this issue? This bug persists and is causing us some problems still.
vaibhavshn commentedon Nov 30, 2022
Have added details about possible memory leak (DOM nodes not being cleaned up) in this issue: #3607 (comment)
alicewriteswrongs commentedon Jun 15, 2023
@tarqwara hello there! I know it's been quite a while since you opened this issue, but I wanted to ask if you were seeing this error outside of running with the dev server?
I'm investigating #3607 today and was looking at this issue as possibly related. After upgrading your reproduction case to
@stencil/[email protected]I was able to reproduce the issue if I used the Stencil dev server (stencil build --dev --watch --serve) but if I instead run a full build and then serve the results, doing something like:I no longer see the issue (meaning, after I 'detach' the element I don't see any
DetachedHTMLElementobjects in a memory snapshot). This makes me suspect that something in the Stencil dev server code is holding on to a reference to those elements.If you're still up for it it would be great if you could confirm under what circumstances you've run into the error!
vaibhavshn commentedon Jun 26, 2023
@alicewriteswrongs looks like it is reproducible in the build output still.
I have a simple example running over at https://stencil-scratchpad.vercel.app.
You can try toggling the component and check the Memory Snapshot.
alicewriteswrongs commentedon Jun 27, 2023
Hey thanks for providing that repro! I did notice that the relevant objects are being logged to the console, which I believe will prevent them from getting garbage collected (see this article for an explanation of that). Would it be possible for you to test if it's reproducible without the logging?
vaibhavshn commentedon Jun 27, 2023
Hmm, looks like you are right. It happens only in the dev server and not in the final build output.
So does it mean that the issue #3607 also happens only in dev server.
Also I am trying to log the hostRefs value without it being logged with my sample but looks like both dev server and production builds work fine, without the memory leak in the simple sample.
alicewriteswrongs commentedon Jun 27, 2023
I think #3607 is a separate issue I think, although unfortunately I still haven't had time to fully dig into it, so I don't think it is dev server specific. It's a complicated one! Garbage collection is supposed to make our lives easy, not harder!
christian-bromann commentedon Jan 10, 2025
A fix for this memory leak has been published in
@stencil/[email protected]. Please let us know if you still see this issue.