Title: HostListener/output() memory leak in SSR · Issue #66506 · angular/angular · GitHub
Open Graph Title: HostListener/output() memory leak in SSR · Issue #66506 · angular/angular
X Title: HostListener/output() memory leak in SSR · Issue #66506 · angular/angular
Description: Which @angular/* package(s) are the source of the bug? Don't known / other Is this a regression? No Description It appears that elements are not being garbage collected in an SSR application if: A child component contains an output() whi...
Open Graph Description: Which @angular/* package(s) are the source of the bug? Don't known / other Is this a regression? No Description It appears that elements are not being garbage collected in an SSR application if: A ...
X Description: Which @angular/* package(s) are the source of the bug? Don't known / other Is this a regression? No Description It appears that elements are not being garbage collected in an SSR application if...
Opengraph URL: https://github.com/angular/angular/issues/66506
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"HostListener/output() memory leak in SSR","articleBody":"### Which @angular/* package(s) are the source of the bug?\n\nDon't known / other\n\n### Is this a regression?\n\nNo\n\n### Description\n\nIt appears that elements are not being garbage collected in an SSR application if: \n* A child component contains an `output()` which it's parent is using\n* A component is using a `@HostListener`\n\n### How to reproduce in provided minimal sample\n* Either start the dev server or run a production build (both have the same result)\n* Open the app\n* Open dev tools\n* Click the toggle button multiple times\n* Take a snapshot of the heap\n* You now see multiple detached instances of both components which are still held in memory even though not used anymore\n\n\u003cimg width=\"2263\" height=\"633\" alt=\"Image\" src=\"https://github.com/user-attachments/assets/92076692-b35a-4db6-9eec-9bc789ef40fe\" />\n\n#### Validation\n* Remove `@HostListener` from `WithHostListener` component\n ```ts\n // Change this\n @HostListener('click', ['$event'])\n public onClick(event: MouseEvent): void {}\n\n // to this\n public onClick(event: MouseEvent): void {}\n\n ```\n* Remove `WithOutput` component's output listener from `App` component\n ```html\n \u003c!-- change this -->\n \u003capp-with-output (myOutput)=\"doSomething()\">\u003c/app-with-output>\n\n \u003c!-- to this -->\n \u003capp-with-output>\u003c/app-with-output>\n ```\n* Run the app\n* Click the toggle button multiple times\n* Take a heap snapshot\n* Now you can see that the DOM Nodes are cleaned up correctly\n\n\u003cimg width=\"2494\" height=\"796\" alt=\"Image\" src=\"https://github.com/user-attachments/assets/c5f9e4ed-9b80-4584-8a5b-e043a2b26aaa\" />\n\n\nChanging the `WithHostListener` component to cleaning up the click listener manually (as seen in the snippet below) also enables the GC to cleanup the `app-with-host-listener` nodes. \n\n```ts\nimport { Component, ElementRef, inject, OnDestroy, Renderer2 } from '@angular/core';\n\n@Component({\n selector: 'app-with-host-listener',\n template: `\n \u003cdiv>With HostListener\u003c/div>\n `,\n})\nexport class WithHostListener implements OnDestroy {\n private readonly renderer = inject(Renderer2);\n private cleanupListener: (() => void) | undefined;\n private readonly el = inject(ElementRef);\n\n constructor() {\n this.cleanupListener = this.renderer.listen(this.el.nativeElement, 'click', event => {\n\t\t\tthis.onClick(event);\n\t\t});\n }\n\n\tpublic onClick(event: MouseEvent): void {console.log(event)}\n\n ngOnDestroy(): void {\n\t\tif (this.cleanupListener) {\n\t\t\tthis.cleanupListener();\n\t\t}\n }\n}\n\n```\n\n### Please provide a link to a minimal reproduction of the bug\n\nhttps://github.com/mii-shell/angular-dom-nodes-memory-leak\n\n### Please provide the exception or error you saw\n\n```true\nNo exception/error was thrown\n```\n\n### Please provide the environment you discovered this bug in (run `ng version`)\n\n```true\nAngular CLI : 21.0.5\nAngular : 21.1.0-rc.0\nNode.js : 24.4.1\nPackage Manager : npm 11.4.2\nOperating System : linux x64\n\n┌───────────────────────────┬───────────────────┬───────────────────┐\n│ Package │ Installed Version │ Requested Version │\n├───────────────────────────┼───────────────────┼───────────────────┤\n│ @angular/build │ 21.1.0-rc.0 │ ^21.1.0-rc.0 │\n│ @angular/cli │ 21.0.5 │ ^21.0.5 │\n│ @angular/common │ 21.1.0-rc.0 │ ^21.1.0-rc.0 │\n│ @angular/compiler │ 21.1.0-rc.0 │ ^21.1.0-rc.0 │\n│ @angular/compiler-cli │ 21.1.0-rc.0 │ ^21.1.0-rc.0 │\n│ @angular/core │ 21.1.0-rc.0 │ ^21.1.0-rc.0 │\n│ @angular/forms │ 21.1.0-rc.0 │ ^21.1.0-rc.0 │\n│ @angular/platform-browser │ 21.1.0-rc.0 │ ^21.1.0-rc.0 │\n│ @angular/platform-server │ 21.1.0-rc.0 │ ^21.1.0-rc.0 │\n│ @angular/router │ 21.1.0-rc.0 │ ^21.1.0-rc.0 │\n│ @angular/ssr │ 21.1.0-rc.0 │ ^21.1.0-rc.0 │\n│ rxjs │ 7.8.2 │ ~7.8.0 │\n│ typescript │ 5.9.3 │ ~5.9.2 │\n│ vitest │ 4.0.17 │ ^4.0.8 │\n└───────────────────────────┴───────────────────┴───────────────────┘\n```\n\n### Anything else?\n\n* I originally thought this problem might be related to #59261, which is why I've used Angular version `21.1.0-rc.0` in my minimal reproduction (which is the version in which this was fixed according to the changelog). Unfortunately, it did not resolve the issue.\n* I've also tested it with version 21.0.5, which resulted in the same\n* Manual garbage collection does not remove the elements from the heap either\n* Identical issue, regardless of whether `@HostListener` or `host` option in `@Component` is used\n* This issue does not occur in a basic Angular app setup without SSR","author":{"url":"https://github.com/mii-shell","@type":"Person","name":"mii-shell"},"datePublished":"2026-01-13T12:49:45Z","url":"https://github.com/angular/angular/issues/66506"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ce2ea554-924b-90c4-cad6-1f8aea64c211 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C1A2:2BE220:301DBCA:4034756:6B01E7F4 |
| html-safe-nonce | c550ce5bc25efe1438e14d893f1c3aa193abe211045c3296e4b2523b0812d8fd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMUEyOjJCRTIyMDozMDFEQkNBOjQwMzQ3NTY6NkIwMUU3RjQiLCJ2aXNpdG9yX2lkIjoiMTQ2NjI2MTA0NzM0NTI3NjkxNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6051aaaa0fcd2ec9d203d780225538c38ee95ac0c50ae661054128cb4db9d5e7 |
| hovercard-subject-tag | issue:3808717756 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/66506/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c26b2e1c356824019e65acd81975742eac1b86da7333bd686f51c9882afafc38/angular/angular/issues/66506 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c26b2e1c356824019e65acd81975742eac1b86da7333bd686f51c9882afafc38/angular/angular/issues/66506 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? Don't known / other Is this a regression? No Description It appears that elements are not being garbage collected in an SSR application if: A ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mii-shell |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width