How to find where my memory leak is in Angular 17 SSR?

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

  1. A subject/Behavior Subject/Observable is subscribed but not unsubscribed on ngOnDestroy in any component.
  2. Subject/Behavior Subject/Observable is being subscribed multiple times(maybe in ngOnChanges or anything like that).

2

  1. 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).

  2. 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:

  1. Every ngFor / @for should have a ‘trackBy’
  2. Every Behavior Subject / Subscription should be unsubscribed either using
    rxjs operators or in ng on destroy
  3. any event listeners using renderer2 should be destroyed.
  4. change detection strategy – if using push, use it wisely.
  5. Any timers / setintervals / setTimeouts, etc should be cleared out.
  6. if using JSON.stringify() or JSON.parse() make sure to use try-catch blocks.
  7. 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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật