Repository navigation
Description
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already report this problem, without success.
Stencil Version
2.18.0
Current Behavior
I am investigating some behaviour I'm seeing in my application (an Ionic application using StencilJS as the framework/runtime) where memory and DOM nodes appear to be leaking. This is occurring when modals are being opened and then dismissed.
I have created a minimal reproduction which you can find here:
https://rococo-creponne-086676.netlify.app/
The reproduction site basically offers two buttons - one that demonstrates the resource leak, and one that demonstrates non-leak behaviour. Both buttons open a modal via the modalController.create method, then immediately dismiss it, and repeat. The component that is created in the modal (app-home) takes one prop, which alters the content it displays. When the "Run Broken" button is pressed, the app-home component renders an <ion-list/> in it's element. When "Run Working" is pressed, the app-home component uses a <div/> instead of an <ion-list/>. The use of div vs ion-list is the only difference (from a user perspective) between the two scenarios.
Once you have pressed "Run Broken" you can see the resource leak via DevTools. Using the Performance Monitor you can see the number of DOM nodes ever increasing, and any attempt to manually trigger a garbage collection does not bring them down. Where as when you choose "Run Working" then the number of DOM nodes will remain stable and triggering a manual GC will immediately flush it back to starting levels.
Taking a Heap Snapshot also shows the leak, where if you inspect the snapshot you will see 100s of Detached elements (ion-modal) instances in the heap.
Tested in Chrome 105.0
I have tried and failed to recreate this within the Ionic source code - my original hunch was that is was some leak in the modal implementation, or perhaps even the ion-list element. But I could not recreate it there which made me believe that it must be more down to the Stencil runtime. Whenever I'm exploring the heap snapshots I come to a dead end at $lazyInstance$ which I think is part of the Stencil runtime, but I am not an expert and couldn't get further.
This is a real issue for us that our users are experiencing - I hope that my code and reproductions are useful and if you need any more info please let me know.
Expected Behavior
Resources (heap memory and DOM nodes) should not leak when opening and closing modals.
Steps to Reproduce
Use the reproduction site (either directly here https://rococo-creponne-086676.netlify.app/ or the code is here https://github.com/joewoodhouse/stencil-modal-memory-investigation
Press "Run Broken"
Monitor DOM nodes and Heap memory
Note: The modal is only visible at a desktop viewport size, I couldn't work out the Ionic reason why
Code Reproduction URL
https://github.com/joewoodhouse/stencil-modal-memory-investigation
https://rococo-creponne-086676.netlify.app/
Additional Information
Using latest Ionic as well (6.2.7)
Activity
joewoodhouse commentedon Sep 14, 2022
I've now refined my reproduction, though I'm not closer to being able to explain what's going on. I've pushed my changes to my repo and Netlify link.
The core seem to be:
<ion-list>element<my-list><my-list/>'srender()function returns<Host class="list-md"/>With the above set of conditions, the leak occurs. If the class list of the
<my-list/>component does not containlist-mdthen the leak does not occur. Equally, if the<ion-list>on the main page is removed, the leak also does not occur.So the leak appears to be due to the interaction between the
<ion-list>and the<my-list>elements, it seems through their CSS. This is where my understanding breaks down unfortunately.My next step will be to try and remove the modal element completely from the reproduction to narrow things down further.
tanner-reits commentedon Sep 19, 2022
@joewoodhouse Thanks for reporting this issue and providing a replication site/repo/steps! Reproduction cases are super helpful for us as we try to pinpoint where some of these obscure issues lie.
That being said, this one could be a doozy. I was able to verify the issue with nodes being infinitely created on your reproduction url. Looks like some garbage collection process is getting hindered. I'll get this one labeled appropriately so it gets pulled into our backlog for work in a future sprint! Thanks for your patience and we'll post any updates here!
vaibhavshn commentedon Nov 30, 2022
I've been debugging this issue.
Only place i could find which still references a node which has been unmounted is the
hostRefsweakmap.Although the
$hostElement$and the$lazyInstanceare set as key in this weakmap, there are still references to this key in the value object.And as per MDN:
This might be preventing the nodes from being garbage collected.
In the following image,
my-testhas been removed from DOM, but the node still exists in the value and key.code where this is set:
https://github.com/ionic-team/stencil/blob/66ce0d330b296ad8ac1ef7ebb5732816af5a6244/src/client/client-host-ref.ts#L32
also, even state values, reference to elements are also not cleaned up because of this.
Solution would be to remove the references to the key from the values, and come up with another way to handle it.
vaibhavshn commentedon Nov 30, 2022
We've seen this memory leak affect our application when we've unmounted a lot of elements, especially things such as tooltips, chat messages are used extensively throughout the app.
joewoodhouse commentedon Nov 30, 2022
@vaibhavshn Nice work, let me know if you need any help. @tanner-reits has there been any internal investigation on this one? Still causing us problems
alicewriteswrongs commentedon Nov 30, 2022
@vaibhavshn thanks for digging into this! It looks like we've got a circular reference between the key and the value within the
WeakMapwhich would I think prevent the key from ever being garbage collected, since the reference to the key held by the value (under$hostElement$) is going to be a strong reference :( so even if it's not referenced anywhere else (e.g. it has been removed from the DOM) I think the garbage collector will continue to skip on by and leave it hanging around since it sees that strong reference.Thank you again for digging into this and finding some key clues! Such things are tricky, and your work will definitely help address the issue. So thanks again! 🔎🧐👍💯
I think I will have time fairly soon to look more into what we can do to address the issue. At minimum, we need to not have a reference to the key in the
WeakMapfrom the value, but I'll need to explore a little bit to see how exactly we can get rid of this. Possibly we can change the reference in the value object to be aWeakRef...Thanks again!
vaibhavshn commentedon Dec 1, 2022
Sure @alicewriteswrongs, do let me know of any timeline you have on the fix.
And do let me know if you need any help from me too.
raymondboswel commentedon Jan 24, 2023
I have another example of a memory leak, here: https://github.com/raymondboswel/stencil-redux-example. The non leak version uses RxJS for state management and the leaky version uses stencil/store, which is the only difference between the leaky and non-leaky versions. Should I create another issue (perhaps on stencil/store?) or is it possibly related to this leak?
48 remaining items
christian-bromann commentedon Oct 3, 2024
Unfortunately it is not that simple. Our team did a lot of experiments with Weakmaps without success. If you have different findings, please share them with us.
spion commentedon Oct 3, 2024
FWIW I actually tried doing the exact same thing a while back (unsetting
$hostRef$on disconnectCallback). The problem is, when I wanted to compare before and after using the original reproduction case - I couldn't reproduce a leak at all anymore.Could anyone confirm whether the original repro above works for them, and if not, does anyone have one that does?
capc0 commentedon Oct 4, 2024
I can confirm that the original reproduction is working for me and still shows the leak. Chrome 129.0.6668.71, Win10.
ghoullier commentedon Dec 9, 2024
Hello!
We also face memory leak on our application.
Why do you not remove the instance from
hostRefin this callback?https://github.com/ionic-team/stencil/blob/main/src/runtime/disconnected-callback.ts#L8-L15
christian-bromann commentedon Dec 9, 2024
That could be a potential option, have you tried if this actually has an impact on the leak?
ghoullier commentedon Dec 10, 2024
We do implement a patch that remove the instance from
hostRefsondisconnectedCallback, it partially fix the memory leak but it also led to other issues... Indeed, we have some stencil components that manipulate other components using the DOM API.appenChild()/.remove().And Stencil raised errors because
getHostRef()returnundefinedfor the components that were detached/attached programatically.So the
disconnectedCallbackis not a good candidate to clean thehostRefs.spion commentedon Dec 10, 2024
FWIW I tried reproducing this w/o using ionic and I couldn't - for me it seems that something similar to ionic dialogs is needed to trigger the issue. If anyone has a successful repro in pure stencil i'd be happy to take another look
vaibhavshn commentedon Dec 12, 2024
We did the same but ended up reverting due to similar issues.
m7mdyahia commentedon Dec 27, 2024
We have the same problem with pure stencil used within Siemens/ix library
raymondboswel commentedon Dec 27, 2024
@m7mdyahia are consumers of the siemens/ix library reporting issues related to this?
christian-bromann commentedon Dec 30, 2024
I am taking a look at this. If anyone can provide additional reproduction cases with the framework of your choice, that would tremendously help with the investigation!
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.