Skip to content

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

Closed

Description

@hiepxanh

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

Activity

  1. hiepxanh commented on Jun 23, 2022

    @hiepxanh
    ContributorAuthor

    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?

  2. hiepxanh commented on Jun 23, 2022

    @hiepxanh
    ContributorAuthor
    • I only make this modification on express engine
      image

    • add appId for render options
      image

    • change renderModule to renderApplication

    image

  3. 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 [/+] on Jun 23, 2022
  4. 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 [/+] on Jun 23, 2022
  5. modified the milestone: needsTriage on Jun 23, 2022
  6. alan-agius4 commented on Jun 23, 2022

    @alan-agius4
    Contributor

    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.

  7. hiepxanh commented on Jun 23, 2022

    @hiepxanh
    ContributorAuthor

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

  8. alxhub commented on Jun 23, 2022

    @alxhub
    Member

    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.

  9. added
    P2The issue is important to a large percentage of users, with a workaround
    on Jun 23, 2022
  10. removed this from the needsTriage milestone on Jun 23, 2022
  11. 7 remaining items

  12. AndrewKushnir commented on Jun 25, 2022

    @AndrewKushnir
    Contributor

    @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.

  13. hiepxanh commented on Jun 25, 2022

    @hiepxanh
    ContributorAuthor

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

  14. hiepxanh commented on Jun 25, 2022

    @hiepxanh
    ContributorAuthor

    @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

  15. AndrewKushnir commented on Jun 27, 2022

    @AndrewKushnir
    Contributor

    @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.

  16. added a commit that references this issue on Jun 28, 2022
    1e7f22f
  17. AndrewKushnir commented on Jun 29, 2022

    @AndrewKushnir
    Contributor

    @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.

  18. hiepxanh commented on Jun 29, 2022

    @hiepxanh
    ContributorAuthor

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

  19. hiepxanh commented on Jun 29, 2022

    @hiepxanh
    ContributorAuthor

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

  20. AndrewKushnir commented on Jun 29, 2022

    @AndrewKushnir
    Contributor

    @hiepxanh great, thanks for the update!

  21. angular-automatic-lock-bot commented on Jul 30, 2022

    @angular-automatic-lock-bot

    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.

  22. locked and limited conversation to collaborators on Jul 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2The issue is important to a large percentage of users, with a workaroundarea: serverIssues related to server-side renderingbugcross-cutting: standaloneIssues related to the NgModule-less worldmemory leakIssue related to a memory leakstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions