Angular not releasing components when using routing?

58 Views Asked by At

I have an app that uses routing to display various pages. I'm having some severe memory leak issues where the app will gradually (or rapidly!) gobble RAM until the browser quits.

I worked with another dev who noticed that the components created don't seem to be released when routing to another component. Is this a thing with angular routing that has to be accounted for somehow?

Notice that, in this minimal reproduction, component1 and component2 are both in the heap snapshot after clicking one, then the other. enter image description here

1

There are 1 best solutions below

3
Mudassar Mustafai On

when you navigate away from the route(component) you should unsubscribe the subscription in the component. you should add observable in the subscription and unsubscribe in the ngOnDestroy life cycle hook.