Sitelet https://web.archive.org/web/20220712012120/https://github.com/angular/angular/issues/46473
Skip to content
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

[platform-server] SSR for Standalone Component: bootstrapApplication have memory leaks #46473

Closed
hiepxanh opened this issue Jun 23, 2022 · 14 comments
Assignees
Labels
bug comp: server cross-cutting: standalone memory leak P2 state: has PR
Milestone

Comments

@hiepxanh
Copy link

@hiepxanh hiepxanh commented Jun 23, 2022 •

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

platform-server

Is this a regression?

No

Description

ngModule version

I clone the Angular Universal repo version from angular:

  • checkout master then: pnpm install
  • pnpm dev:ssr to serve
  • nodemon --inspect=localhost:9236 dist/server/main.js to inspect

Then I start it and make a heap snapshot. it's fine.
Open dev tools for Node.js to inspect, Go to memory tab, search module keyword
image

first-time load home page:
image
load home page at second time:
image

load home page at 20th times:
image

everything is fine and no leak. NgModule only have 2 instance

Standalone version

I upgrade the universal version of the standalone component, I'm going to make this on the universal repo, but first, it has a memory leak.

  • checkout master then: pnpm install
  • pnpm dev:ssr to serve
  • nodemon --inspect=localhost:9236 dist/server/main.js to inspect

go home page and f5 for 6 times:
image

go home page and f5 for more 5 times:
image

we get too many modules that are not clear and the heap memory goes "To infinity and beyond".
The ApplicationModule goes to x6 on 6 load pages, and x11 if you have 11 load pages.

Please provide a link to a minimal reproduction of the bug

https://github.com/hiepxanh/universal-test

Please provide the exception or error you saw

No response

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

Angular CLI: 14.0.2
Node: 16.13.1
Package Manager: pnpm 7.1.5
OS: win32 x64

Angular: 14.0.3
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1400.2
@angular-devkit/build-angular   14.0.2
@angular-devkit/core            14.0.2
@angular-devkit/schematics      14.0.2
@angular/cli                    14.0.2
@nguniversal/builders           14.0.1
@nguniversal/common             14.0.1
@nguniversal/express-engine     14.0.1
@schematics/angular             14.0.2
rxjs                            7.5.5
typescript                      4.7.4

Anything else?

I'm running the standalone SSR version in my production version. Current work around is restart the server :D

@hiepxanh
Copy link
Author

@hiepxanh hiepxanh commented Jun 23, 2022

in my production app, it goes really bad, it takes 1 day only to eat my 1 GB ram.

image

image

what is the best solution to config, or is it a bug?

@hiepxanh
Copy link
Author

@hiepxanh hiepxanh commented Jun 23, 2022 •

  • I only make this modification on express engine
    image

  • add appId for render options
    image

  • change renderModule to renderApplication

image

@hiepxanh hiepxanh changed the title [platform-server] bootstrapApplication have memory leaks, the module generate each time server render [platform-server] SSR for standalone component: bootstrapApplication have memory leaks, the module generate each time server render Jun 23, 2022
@hiepxanh hiepxanh changed the title [platform-server] SSR for standalone component: bootstrapApplication have memory leaks, the module generate each time server render [platform-server] SSR for Standalone Component: bootstrapApplication have memory leaks Jun 23, 2022
@pkozlowski-opensource pkozlowski-opensource added comp: server cross-cutting: standalone labels Jun 23, 2022
@ngbot ngbot bot added this to the needsTriage milestone Jun 23, 2022
@ngbot ngbot bot added this to the needsTriage milestone Jun 23, 2022
@alan-agius4
Copy link
Contributor

@alan-agius4 alan-agius4 commented Jun 23, 2022

in my production app, it goes really bad, it takes 1 day only to eat my 1 GB ram.

Side note: this API is still experimental and should not be used in production.

@pkozlowski-opensource pkozlowski-opensource added the memory leak label Jun 23, 2022
@hiepxanh
Copy link
Author

@hiepxanh hiepxanh commented Jun 23, 2022

sorry, I was tempted. It just too smooth. I used jsdom too, but it event eat more ram than this one.

@alxhub
Copy link
Contributor

@alxhub alxhub commented Jun 23, 2022

I believe you're right about the leak. Digging into this just a little, it appears that @angular/platform-server's _render is relying on destroying the platform to destroy the application, and the platform seems to rely on registration of bootstrapped NgModules, so indeed this might not be correctly destroying the rendered application.

@alxhub alxhub added P2 bug labels Jun 23, 2022
@ngbot ngbot bot removed this from the needsTriage milestone Jun 23, 2022
@ngbot ngbot bot added this to the Backlog milestone Jun 23, 2022
@ngbot ngbot bot removed this from the needsTriage milestone Jun 23, 2022
@ngbot ngbot bot added this to the Backlog milestone Jun 23, 2022
@hiepxanh
Copy link
Author

@hiepxanh hiepxanh commented Jun 23, 2022

Yes, I do, I also put a log while testing but do not see that log trigger on ngOnDestroy of appComponent

@AndrewKushnir AndrewKushnir self-assigned this Jun 24, 2022
AndrewKushnir added a commit to AndrewKushnir/angular that referenced this issue Jun 25, 2022
This commit updates the `ApplicationRef` logic to trigger the destroy operation when an underlying platform is destroyed. This is needed to make sure all teardown processing is completed correctly to avoid memory leaks.

Closes angular#46473.
AndrewKushnir added a commit to AndrewKushnir/angular that referenced this issue Jun 25, 2022
This commit updates the `ApplicationRef` logic to trigger the destroy operation when an underlying platform is destroyed. This is needed to make sure all teardown processing is completed correctly to avoid memory leaks.

Closes angular#46473.
@AndrewKushnir
Copy link
Contributor

@AndrewKushnir AndrewKushnir commented Jun 25, 2022

@hiepxanh I've created a PR with a proposed fix, which ensures that the ApplicationRef.destroy is called (and subsequently clears references) when a platform is destroyed. I'd like to ask if you could try using a package from that PR and let us know if the memory leaks is gone. You can update the package.json in your app like this (and run npm i):

  "@angular/core": "https://output.circle-artifacts.com/output/job/9387cf04-2cb0-47ad-b868-42121b6a011f/artifacts/0/angular/core-pr46497-91c5f1807c.tgz",

Note: this package is for testing purposes only and can not be used in production.

Thank you.

@hiepxanh
Copy link
Author

@hiepxanh hiepxanh commented Jun 25, 2022

@AndrewKushnir yes sir, I'm very exciting, I'll test on this now

@hiepxanh
Copy link
Author

@hiepxanh hiepxanh commented Jun 25, 2022

@AndrewKushnir thank you, sir. There is no sign of the memory leak anymore.
image

After routing about 100 routes, the memory is still stable. everything is fine. I think I can use it now <3

@AndrewKushnir
Copy link
Contributor

@AndrewKushnir AndrewKushnir commented Jun 27, 2022

@hiepxanh thanks for testing the change! We'll follow our regular dev process (review, testing) and the fix will land in one of the upcoming patch versions (14.0.x). Thank you.

AndrewKushnir added a commit to AndrewKushnir/angular that referenced this issue Jun 27, 2022
This commit updates the `ApplicationRef` logic to trigger the destroy operation when an underlying platform is destroyed. This is needed to make sure all teardown processing is completed correctly to avoid memory leaks.

Closes angular#46473.
AndrewKushnir added a commit to AndrewKushnir/angular that referenced this issue Jun 28, 2022
This commit updates the `ApplicationRef` logic to trigger the destroy operation when an underlying platform is destroyed. This is needed to make sure all teardown processing is completed correctly to avoid memory leaks.

Closes angular#46473.
dylhunn pushed a commit that referenced this issue Jun 28, 2022
#46497)

This commit updates the `ApplicationRef` logic to trigger the destroy operation when an underlying platform is destroyed. This is needed to make sure all teardown processing is completed correctly to avoid memory leaks.

Closes #46473.

PR Close #46497
@AndrewKushnir
Copy link
Contributor

@AndrewKushnir AndrewKushnir commented Jun 29, 2022

@hiepxanh just wanted to let you know that we've released Angular v14.0.4, which contains the memory leak fix. Please try updating to the latest version and let us know if the problem is resolved. Thank you.

@hiepxanh
Copy link
Author

@hiepxanh hiepxanh commented Jun 29, 2022

Thank you that so fast, I'm going to do it now 😍

@hiepxanh
Copy link
Author

@hiepxanh hiepxanh commented Jun 29, 2022

@AndrewKushnir I already test it, and I see the result is very good. Thank you for your effor
image

@AndrewKushnir
Copy link
Contributor

@AndrewKushnir AndrewKushnir commented Jun 29, 2022

@hiepxanh great, thanks for the update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug comp: server cross-cutting: standalone memory leak P2 state: has PR
Projects
None yet
Development

No branches or pull requests

5 participants