question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Components not being garbage collected by browser, causing major memory leak

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

Don’t know / other

Is this a regression?

No

Description

Stack Overflow Post: https://stackoverflow.com/questions/71026959/memory-leak-garbage-collection-in-browser-not-collecting-components

Summary:

  • when a component gets created with *ngIf=“true” or with ViewContainerRef.createComponent(componentFactory: ComponentFactory<C>), and then destroyed, with *ngIf=“false” or with ViewContainerRef.clear(), the component stays in memory, and can’t be garbage collected by the browser
  • this causes a significant memory leak (especially in larger apps that use many nested components)
  • this is a tricky bug because:
    • if you download the minimal reproduction app, either a or b will happen a. it will leak memory right away, and components won’t be garbage collected (regularly or with the manual garbage collection button provided by most browsers for debugging purposes) b. it won’t leak memory right away, and it will happily garbage collect components regularly or with the manual garbage collection button provided by most browsers for debugging. After enough time has elapsed, or after you’ve restarted the app an arbitrary amount of times, it will eventually get into the memory leaking state
    • therefore, if my coworkers and I download the repo, then run the app, it seems to be a coin toss on whether or not this bug gets reproduced right away
    • if you’re one of the lucky few that doesn’t get it to happen right away, perhaps try it on a different computer, or wait a couple minutes and refresh the web app a couple times
  • the fact that it doesn’t happen first try every time makes it hard to prove that there is a bug with this minimal reproduction app
    • I’ve included screen shots in the stack overflow post (near the bottom), that proves that this is indeed happening
    • it does however happen every time with a much larger production app that I’m working on

Please provide a link to a minimal reproduction of the bug

Repo: https://github.com/kevinpbaker/angular-memory-killer S3 bucketed app: https://angular-memory-killer.s3.us-west-2.amazonaws.com/index.html

Please provide the exception or error you saw

There is no error thrown. The components in memory aren't being garbage collected, and will eventually crash the browser window.

Please provide the environment you discovered this bug in (run ng version)

I have upgraded this project to be the latest version of angular 12, and the latest version of angular 13, and it leaks in 11, 12, and 13. I have also tried it on multiple computers (M1 mac, older intel mac, windows pc, windows laptop).

Angular CLI: 11.2.18
Node: 16.14.0
OS: darwin arm64

Angular: 11.2.14
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1102.18
@angular-devkit/build-angular   0.1102.18
@angular-devkit/core            11.2.18
@angular-devkit/schematics      11.2.18
@angular/cdk                    11.2.13
@angular/cli                    11.2.18
@angular/material               11.2.13
@schematics/angular             11.2.18
@schematics/update              0.1102.18
rxjs                            6.5.5
typescript                      4.0.8

Anything else?

I’ve included a link to a stack overflow post I made that adds additional information and pictures to show that it is indeed leaking memory.

Microsoft Edge has an experimental feature called “Detached Elements” that can be enabled to see any objects that are dangling and ready to be garbage collected. You can use this feature to visually see that the angular components aren’t being garbage collected (I’ve also included screen shots of this in the stack overflow post).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:21 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
AndrewKushnircommented, Apr 6, 2022

@kevinpbaker thanks for the update!

1reaction
kevinpbakercommented, Apr 6, 2022

Chromium bug: https://bugs.chromium.org/p/chromium/issues/detail?id=1308845

This issue has been fixed within the chromium browser. It hasn’t been released yet, but it can be tested with Chrome’s Canary browser: https://www.google.com/intl/en_ca/chrome/canary/

Broken Version 100.0.4896.60 (Official Build) (arm64)

https://user-images.githubusercontent.com/48214072/162014156-d33d5521-a5fc-4554-ade2-c7a943923a16.mp4

Fixed Version 102.0.4987.0 (Official Build) canary (arm64)

https://user-images.githubusercontent.com/48214072/162014327-751da40f-35fd-429a-8a1c-9fcc32225b43.mp4

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular - Memory Leak - Garbage Collection in Browser Not ...
The leak happens because LayoutInline gets added to TextAutoresizer::fingerprints_ but is never removed. LayoutBlocks gets removed because it ...
Read more >
Fix memory problems - Chrome Developers
Identify detached DOM trees (a common cause of memory leaks) with Heap Snapshots ... Garbage collection is when the browser reclaims memory.
Read more >
Causes of Memory Leaks in JavaScript and How to Avoid Them
The browser keeps objects in heap memory while they can be reached from the root through the reference chain. Garbage Collector is a...
Read more >
Hunting Memory Leakage in JavaScript Application
Memory leakage is defined as a situation when the memory not needed by the program is not returned to the operating system or...
Read more >
Preventing and detecting memory leaks in Android apps
Memory leaks occur when an object that is supposed to be garbage collected has something holding a reference to it. As more and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found