Skip to content

bug: Memory leak with detached DOM nodes leading to infinite growth #30215

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

Open
3 tasks done
ralphcode opened this issue Feb 25, 2025 · 9 comments
Open
3 tasks done

bug: Memory leak with detached DOM nodes leading to infinite growth #30215

ralphcode opened this issue Feb 25, 2025 · 9 comments
Labels
needs: reply the issue needs a response from the user

Comments

@ralphcode
Copy link

ralphcode commented Feb 25, 2025

Prerequisites

Ionic Framework Version

v8.x

Current Behavior

Navigating between pages in an Ionic + Angular application leads to a steadily increasing DOM node count. Over time, this can balloon to tens of thousands of detached DOM nodes (verified via Chrome DevTools), causing performance degradation and application crashes.

Expected Behavior

When a component or page is destroyed/navigated away from, its DOM elements should be properly removed from memory. The DOM node count should not grow indefinitely, ensuring stable performance over time.

Steps to Reproduce

  1. Clone code reproduction URL + npm install + npm start
  2. Open Chrome DevTools → Performance & Memory tab.
  3. Repeatedly navigate next, then back to root. Each will load 5000 nodes.
  4. Note, ngOnDestroy successfully executes
  5. Observe the DOM node count growing with each navigation in the Performance Monitor, and detached DOM nodes in the Memory tab snapshots. Forcing Garbage collection has no effect.
  6. Snapshotting at root, and then again at root after navigation shows Detached <div> of the exact count with set Objects allocated between Snapshot X and Snapshot X
Screen.Recording.2025-02-26.at.9.40.18.am.mov

Code Reproduction URL

https://github.com/ralphcode/capacitor-angular-dom-leak

Ionic Info

Ionic:

   Ionic CLI                     : 7.2.0
   Ionic Framework               : @ionic/angular 8.5.0
   @angular-devkit/build-angular : 18.2.14
   @angular-devkit/schematics    : 18.2.14
   @angular/cli                  : 18.2.14
   @ionic/angular-toolkit        : 12.1.1

Utility:

   cordova-res                          : not installed globally
   native-run (update available: 2.0.1) : 1.4.0

System:

   NodeJS : v18.20.3
   npm    : 10.7.0
   OS     : macOS Unknown

Additional Information

Possibly related to: #30132 (specifically Resolved performance issues due to detached nodes in memory) and here within Stencil/Core: stenciljs/core#6210

If you need anything further, please let me know

@ralphcode
Copy link
Author

ralphcode commented Mar 14, 2025

Fyi, this memory leak is still present in 8.5.1

@KeyTurns
Copy link

They must fix this asap, the users are getting warnings from the browser because of to much RAM usage!

@Noaber
Copy link

Noaber commented Apr 11, 2025

Any updates ? @brandyscarney ?

@ShaneK
Copy link
Member

ShaneK commented Apr 14, 2025

Hello! I believe what you're seeing is an issue with Chrome's dev tools where having them open while doing tasks will keep nodes attached and then they'll show up as detached nodes. Please try to repeat your experience with dev tools closed, then open them and look for detached nodes. If you can reproduce them growing infinitely without dev tools open while navigating the page, then it is a bug.

@ShaneK ShaneK added needs: reply the issue needs a response from the user and removed triage labels Apr 14, 2025
@ShaneK
Copy link
Member

ShaneK commented Apr 14, 2025

For reference, I believe the issue with the performance monitor isn't really any issue, it's just how the performance monitor grabs and holds on to references. Here's it being pointed out in another bug report in React. (Related Chromium bug report)

The other issue with detached nodes may be related to this recently closed issue in Chromium, but I won't be sure until that gets into mainstream Chrome.

@ionitron-bot ionitron-bot bot added triage and removed needs: reply the issue needs a response from the user labels Apr 14, 2025
@ShaneK ShaneK added needs: reply the issue needs a response from the user and removed triage labels Apr 14, 2025
@ralphcode
Copy link
Author

Hey @ShaneK thanks for the input. The Chrome report claims this was fixed in v136.0.7102.0 but I've tested this in Version v137.0.7123.0 (Official Build) canary (arm64) and the exact same problem with Angular Ionic on the above reproduction repo:

Image

@ionitron-bot ionitron-bot bot added triage and removed needs: reply the issue needs a response from the user labels Apr 14, 2025
@ralphcode
Copy link
Author

@ShaneK I've also tested 8.5.4-dev.11744209290.123af9eb (#30132) with Chrome Version 137.0.7123.0 (Official Build) canary (arm64) and the same issue.

@ShaneK
Copy link
Member

ShaneK commented Apr 15, 2025

@ralphcode That may be the case, I wasn't sure if that bug report would resolve this particular issue or not - it was the closest I could find. However, without being able to replicate this issue of detached nodes without Chrome dev tools being open - which I cannot - then this is not a bug in Ionic Framework. It's just a quirk of Chrome dev tools capturing node references while it's open.

Screenshot-2025-04-15.at.06.25.46.mp4

I'm not saying there's not a memory leak in your app - there may be - but this Chrome dev tools issue will make it difficult to find. All I can say is I've done extensive testing around reported memory leaks that have been based around this same issue with Chrome over the last 2 weeks and they all end up leading back to Chrome's internal tooling being the reference holders.

There are actually two cases I know of in Ionic Framework that do create real actual detached nodes that can be verified in this way, but they do not create them endlessly and are not actually memory leaks because of that. They're just DOM elements that need to be removed and so are detached. We may address how these work in the future, but for now, they're not a priority because they're not actually causing leaks.

@ShaneK ShaneK added the needs: reply the issue needs a response from the user label Apr 15, 2025
@ionitron-bot ionitron-bot bot removed the triage label Apr 15, 2025
@ralphcode
Copy link
Author

Thanks for the follow-up. That makes sense. I'll do some tests in Xcode Instruments to double-check actual memory usage at the OS level and see if it keeps growing or stabilizes. It won't show the same DOM node details as Chrome dev tools, but with this example the memory footprint should balloon there, if not it's likely just the tooling reference issue you suspect rather than a true leak.

I'm under the pump at the moment, so I'll try to run those diagnostics soon and let you know what I find. Really appreciate your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: reply the issue needs a response from the user
Projects
None yet
Development

No branches or pull requests

4 participants