Full disclosure: The tool mentioned below was developed by me.
To achieve this, create a list to store new subscriptions and remove them once they are unsubscribed.
The challenging aspect is monitoring subscriptions. One way to do this is by modifying the Observable#subscribe() method through monkey-patching, essentially replacing the Observable prototype method.
This is the general concept behind observable-profiler, a tool that integrates with an Observable library like rxjs and displays subscription leaks in the console.
An easy way to utilize the profiler is to initiate tracking when the application is launched and then end tracking after a specific duration:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { Observable } from 'rxjs';
import { setup, track, printSubscribers } from 'observable-profiler';
setup(Observable);
platformBrowserDynamic([])
.bootstrapModule(AppModule)
.then(ref => {
track();
window.stopProfiler = () => {
ref.destroy();
const subscribers = track(false);
printSubscribers({
subscribers,
});
}
});
To generate a report, simply execute stopProfiler()
in the developer tools console.