Repository navigation
Description
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:ssrto servenodemon --inspect=localhost:9236 dist/server/main.jsto 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

first-time load home page:

load home page at second time:

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:ssrto servenodemon --inspect=localhost:9236 dist/server/main.jsto inspect
go home page and f5 for 6 times:

go home page and f5 for more 5 times:

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

Activity
hiepxanh commentedon Jun 23, 2022
in my production app, it goes really bad, it takes 1 day only to eat my 1 GB ram.
what is the best solution to config, or is it a bug?
hiepxanh commentedon Jun 23, 2022
I only make this modification on express engine

add appId for render options

change renderModule to renderApplication
[-][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 [/+][-][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 [/+]alan-agius4 commentedon Jun 23, 2022
Side note: this API is still experimental and should not be used in production.
hiepxanh commentedon 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 commentedon Jun 23, 2022
I believe you're right about the leak. Digging into this just a little, it appears that
@angular/platform-server's_renderis 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.7 remaining items
AndrewKushnir commentedon Jun 25, 2022
@hiepxanh I've created a PR with a proposed fix, which ensures that the
ApplicationRef.destroyis 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 thepackage.jsonin your app like this (and runnpm i):Note: this package is for testing purposes only and can not be used in production.
Thank you.
hiepxanh commentedon Jun 25, 2022
@AndrewKushnir yes sir, I'm very exciting, I'll test on this now
hiepxanh commentedon Jun 25, 2022
@AndrewKushnir thank you, sir. There is no sign of the memory leak anymore.

After routing about 100 routes, the memory is still stable. everything is fine. I think I can use it now <3
AndrewKushnir commentedon 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.
fix(core): trigger `ApplicationRef.destroy` when Platform is destroyed (
AndrewKushnir commentedon 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 commentedon Jun 29, 2022
Thank you that so fast, I'm going to do it now 😍
hiepxanh commentedon Jun 29, 2022
@AndrewKushnir I already test it, and I see the result is very good. Thank you for your effor

AndrewKushnir commentedon Jun 29, 2022
@hiepxanh great, thanks for the update!
angular-automatic-lock-bot commentedon Jul 30, 2022
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.