Skip to content

bug: Potential Memory/Resource Leak #3607

Closed

Description

@joewoodhouse

Prerequisites

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/

Code here:
https://github.com/joewoodhouse/stencil-modal-memory-investigation

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

  1. joewoodhouse commented on Sep 14, 2022

    @joewoodhouse
    ContributorAuthor

    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:

    • The app main page contains an <ion-list> element
    • A modal is presented
    • The modal's component renders a custom component called <my-list>
    • The <my-list/>'s render() 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 contain list-md then 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.

  2. self-assigned this
    on Sep 19, 2022
  3. tanner-reits commented on Sep 19, 2022

    @tanner-reits
    Contributor

    @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!

  4. added
    Bug: Validated
    and removed on Sep 19, 2022
  5. removed their assignment
    on Sep 19, 2022
  6. added
    Bug: Validated
    and removed
    Bug: Validated
    on Nov 3, 2022
  7. vaibhavshn commented on Nov 30, 2022

    @vaibhavshn

    I've been debugging this issue.

    Only place i could find which still references a node which has been unmounted is the hostRefs weakmap.
    Although the $hostElement$ and the $lazyInstance are set as key in this weakmap, there are still references to this key in the value object.

    And as per MDN:

    A WeakMap is a collection of key/value pairs whose keys must be objects, with values of any arbitrary JavaScript type, and which does not create strong references to its keys.

    This might be preventing the nodes from being garbage collected.

    In the following image, my-test has been removed from DOM, but the node still exists in the value and key.

    image

    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.

  8. vaibhavshn commented on Nov 30, 2022

    @vaibhavshn

    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.

  9. joewoodhouse commented on Nov 30, 2022

    @joewoodhouse
    ContributorAuthor

    @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

  10. alicewriteswrongs commented on Nov 30, 2022

    @alicewriteswrongs
    Contributor

    @vaibhavshn thanks for digging into this! It looks like we've got a circular reference between the key and the value within the WeakMap which 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 WeakMap from 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 a WeakRef...

    Thanks again!

  11. vaibhavshn commented on Dec 1, 2022

    @vaibhavshn

    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.

  12. raymondboswel commented on Jan 24, 2023

    @raymondboswel

    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?

  13. 48 remaining items

  14. christian-bromann commented on Oct 3, 2024

    @christian-bromann
    Member

    Why is it not just that simple?

    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.

  15. spion commented on Oct 3, 2024

    @spion

    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?

  16. capc0 commented on Oct 4, 2024

    @capc0

    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?

    I can confirm that the original reproduction is working for me and still shows the leak. Chrome 129.0.6668.71, Win10.

    image

    image

  17. ghoullier commented on Dec 9, 2024

    @ghoullier

    Hello!

    We also face memory leak on our application.

    Why do you not remove the instance from hostRef in this callback?

    https://github.com/ionic-team/stencil/blob/main/src/runtime/disconnected-callback.ts#L8-L15

  18. christian-bromann commented on Dec 9, 2024

    @christian-bromann
    Member

    Why do you not remove the instance from hostRef in this callback?

    That could be a potential option, have you tried if this actually has an impact on the leak?

  19. ghoullier commented on Dec 10, 2024

    @ghoullier

    We do implement a patch that remove the instance from hostRefs on disconnectedCallback, 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() return undefined for the components that were detached/attached programatically.
    So the disconnectedCallback is not a good candidate to clean the hostRefs.

  20. spion commented on Dec 10, 2024

    @spion

    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

  21. vaibhavshn commented on Dec 12, 2024

    @vaibhavshn

    We do implement a patch that remove the instance from hostRefs on disconnectedCallback, it partially fix the memory leak but it also led to other issues.

    We did the same but ended up reverting due to similar issues.

  22. m7mdyahia commented on Dec 27, 2024

    @m7mdyahia

    If anyone has a successful repro in pure stencil i'd be happy to take another look

    We have the same problem with pure stencil used within Siemens/ix library

  23. raymondboswel commented on Dec 27, 2024

    @raymondboswel

    @m7mdyahia are consumers of the siemens/ix library reporting issues related to this?

  24. christian-bromann commented on Dec 30, 2024

    @christian-bromann
    Member

    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!

  25. christian-bromann commented on Jan 10, 2025

    @christian-bromann
    Member

    A fix for this memory leak has been published in @stencil/[email protected]. Please let us know if you still see this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Bug: Validated

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions