SAP / spartacus

Spartacus is a lean, Angular-based JavaScript storefront for SAP Commerce Cloud that communicates exclusively through the Commerce REST API.
Apache License 2.0
744 stars 388 forks source link

Memory leak when running Angular Universal #16525

Open ESchuderer opened 2 years ago

ESchuderer commented 2 years ago

Describe the bug SAP Spartacus memory consumption doesn't free up with a few continuous concurrent requests (10+ requests). This causes the node.js process to run out of memory and all SSR requests to time out.

Memory grows quickly even for simple operations like opening the homepage, indicating a memory leak.

Tell us the version of Spartacus

To Reproduce Steps to reproduce the behavior:

  1. Setup a new spartacus storefront with Angular 12 and Spartacus 4.3.x
  2. Setup SSR
  3. Run load tests against the fresh Spartacus installation

Expected behavior Memory consumption stays low

Screenshots Screenshot 2022-11-22 at 18 32 20

Additional context Setting the cacheSize configuration according to https://sap.github.io/spartacus-docs/server-side-rendering-optimization/#configuring-the-ssr-optimization-engine helps to mitigate the issue, preventing OOM errors.

But SSR will still time out and redirect to CSR while impacting performance.

Setting the property inlineCriticalCss introduced by Angular 12 didn't mitigate the issue:

server.engine(
    'html',
    ngExpressEngine({
      bootstrap: AppServerModule,
      inlineCriticalCss: false
    }),
  );
ESchuderer commented 2 years ago

Useful linting tools to prevent such issues: https://github.com/cartant/eslint-plugin-rxjs https://github.com/cartant/eslint-plugin-rxjs-angular

Useful operator to unsubscribe on destroy: https://github.com/ngneat/until-destroy

ESchuderer commented 2 years ago

Potential leak in PageLayoutComponent as the Subject is not completed

Solution, call complete (not unsubscribe!) during destruction

ngOnDestroy(): void {
    this.section$.complete();
  }
ESchuderer commented 2 years ago

Observable profiler to check mem leaks: https://github.com/awerlang/observable-profiler

ESchuderer commented 2 years ago

Screenshot 2022-11-23 at 01 10 25