I’ve got a memory leak in my production environment with Angular SSR. Essentially, every time a page is refreshed, it leaks.
I see HTMLBaseElement
still being referenced.
And I see R3Injector still being referenced.
I’ve unsubscribed all the observables and I’ve updated zone as per angular ssr issue, but I still see an increase in memory usage.
What is interesting, is that I see CSS being in memory and I don’t see that making much sense.
Any ideas of what I should be trying?
Package.json dependencies are:
"dependencies": {
"@angular/animations": "^17.0.2",
"@angular/cdk": "^17.3.10",
"@angular/common": "^17.0.2",
"@angular/compiler": "^17.0.2",
"@angular/core": "^17.0.2",
"@angular/forms": "^17.0.2",
"@angular/platform-browser": "^17.0.2",
"@angular/platform-browser-dynamic": "^17.0.2",
"@angular/platform-server": "^17.0.2",
"@angular/router": "^17.0.2",
"@angular/ssr": "^17.0.0",
"@ng-bootstrap/ng-bootstrap": "^16.0.0",
"@ngrx/effects": "^17.0.1",
"@ngrx/store": "^17.0.1",
"@ngrx/store-devtools": "^17.0.1",
"@popperjs/core": "^2.11.6",
"@stripe/stripe-js": "^3.1.0",
"@types/intercom-web": "^2.8.24",
"angular-google-tag-manager": "^1.9.0",
"bootstrap": "^5.2.3",
"express": "^4.18.2",
"http-proxy": "^1.18.1",
"jquery": "^3.7.1",
"luxon": "^3.4.3",
"ng-recaptcha": "^13.1.1",
"ngx-cookie-service": "^17.1.0",
"ngx-slick-carousel": "^17.0.0",
"rxjs": "~7.8.0",
"slick-carousel": "^1.8.1",
"tslib": "^2.3.0",
"zone.js": "~0.14.8"
},
1
Based on your screenshot, I don’t have enough information to tell you which one is leaking but I can give you a strategy to find the culprit, not the fastest one but usually the most guaranteed one.
Try a divide and conquer approach:
- Pages: Remove one by one page in your route, check if the leak still happened. If the leak stopped, you found the page that cause leak, now you can lower the scope to that page only
- Components: Remove one by one in that page, repeated until you found the component.
- Inside the component, assuming you are using standalone, check all the pipe, service, or other component that you are importing, remove until the leak stop. Check the one that cause the leak and fix it.
Usually need to find out if you are using setTimeout, setInterval or timer of rxjs, also check for cycle import(usually you will get noticed by your IDE), check if you do any dom manipulation inside ngOnChanges or ngDoCheck, these two life cycle run a lot and can cause serious problem if you don’t check carefully.
My approach will take you a lot of time but if you have no other choice, just try it. If you have a working version without leaking before this version. You can reduce the scope to read quicker based on what you added from the working version until this version.
There can be many reasons for memory leak in angular, check for below mentioned things in code to find memory leaks
- A subject/Behavior Subject/Observable is subscribed but not unsubscribed on ngOnDestroy in any component.
- Subject/Behavior Subject/Observable is being subscribed multiple times(maybe in ngOnChanges or anything like that).
2
-
Your screenshots show that some HTML elements (HTMLBaseElement)
are still in memory even after you refresh the page. This could mean
that these elements are not getting properly removed when they
should be Check if your components are properly cleaning up these
elements when they’re no longer needed. Specifically make sure any
changes you make to the page’s structure are undone when the
component is destroyed (using the ngOnDestroy function). -
You mentioned that CSS is staying in memory. Check if you’re adding styles
dynamically (using things like ngClass). If so, make sure they’re being
properly cleaned up when no longer needed.
3
Just a suggestion:
Basic checks include but not limited to:
- Every ngFor / @for should have a ‘trackBy’
- Every Behavior Subject / Subscription should be unsubscribed either using
rxjs operators or in ng on destroy - any event listeners using renderer2 should be destroyed.
- change detection strategy – if using push, use it wisely.
- Any timers / setintervals / setTimeouts, etc should be cleared out.
- if using JSON.stringify() or JSON.parse() make sure to use try-catch blocks.
- Any API retries, make sure to have a catchError block and keep the retries count to either 5 or max 10 nothing more. Even 10 is too high