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.

Ivy doesn’t deallocate memory of destroyed components correctly

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/core@9.0.0-rc.14, earlier versions probably as well.

Is this a regression?

This issue does not occur in Angular 8 + View Engine.

Description

Ivy does not seem to correctly deallocate memory of destroyed components in all cases. We’ve noticed an issue that when navigating away from a route, at least some of the loaded components are getting destroyed but not deallocated and garbage collected. (The issue might not be restricted to routing.)

The problem is that unnecessary memory is not deallocated, which might lead to problems on mobile and other low-memory devices and might also have security implications (e.g. when sensitive state remains in memory, even after a user has logged out).

🔬 Minimal Reproduction

Here is a minimal repo of the problem. It’s a plain Angular CLI application with three routes and four components:

  • /#/route1 (Route1Component) includes the ContentComponent as a child that loads the contents of a large file retrieved via the network into a field of its class during ngOnInit, consuming some memory. This component logs to the console when it’s being initialized or destroyed.
  • /#/route2 (Route2Component) and route3 (Route3Component) don’t include this component.

Repro steps:

git clone https://github.com/thinktecture-labs/ivy-mem-leak.git
cd ivy-mem-leak
npm i
npm start

Stop ng serve and compare this with the behavior of Angular 8:

git checkout ng8
npm i
npm start

🔥 Exception or Error

The memory of route1’s components doesn’t seem to be correctly deallocated when leaving route1 for route2 on Angular 9/Ivy:

Angular 9/Ivy Angular 8/View Engine
image image

As per DevTools, (at least) the component instance of the ContentComponent seems to be retained by a contextLView array even after it has been destroyed:

image

Is this behavior intended or is this a bug?

Researched by @ManuelRauber, @thomashilzendegen and me.

🌍 Your Environment

Angular Version:


     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 9.0.0-rc.12
Node: 12.14.1
OS: darwin x64

Angular: 9.0.0-rc.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.900.0-rc.12
@angular-devkit/build-angular     0.900.0-rc.12
@angular-devkit/build-optimizer   0.900.0-rc.12
@angular-devkit/build-webpack     0.900.0-rc.12
@angular-devkit/core              9.0.0-rc.12
@angular-devkit/schematics        9.0.0-rc.12
@angular/cli                      9.0.0-rc.12
@ngtools/webpack                  9.0.0-rc.12
@schematics/angular               9.0.0-rc.12
@schematics/update                0.900.0-rc.12
rxjs                              6.5.4
typescript                        3.7.5
webpack                           4.41.2

Anything else relevant?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
mheverycommented, Feb 5, 2020

Kudos for such a well documented repro steps. I wish more people would provide examples such as these.

It looks to me that master is also on Ivy and when I try to reproduce the working example it also retains on master could you please fix that?

1reaction
mheverycommented, Feb 6, 2020

@mhevery In the repro, the master branch is Angular 9/Ivy and the ng8 branch is Angular 8/View Engine. As you’ve provided a potential fix, I guess we don’t have to update the repro repo?

no need to do anything.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solving Memory Leaks Issues in Ionic Angular - Cartona
We found that in Angular 9, Ivy is leaking memory and it doesn't deallocate memory of destroyed components correctly. In this issue you...
Read more >
Angular not releasing memory on component destroy
1 · I came to ask the same question, in my experience, this usually means something is still subscribed · 2 · "if...
Read more >
Lazy load Angular components - Medium
In the “Component Code” section, we can see that Ivy keeps our component code. Nothing special. But then Ivy also adds some metadata...
Read more >
Hooking into the component lifecycle - Angular Hispano
A component instance has a lifecycle that starts when Angular instantiates the component class and renders the component view along with its child...
Read more >
Release notes and release history for Mobiscroll
We fixed an issue where some resources were not showing up when printing the timeline component. We fixed an issue in angular that...
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