If I have a test such as the following:
it('should return some observable', async(() => {
mockBackend.connections.subscribe((mockConnection: MockConnection) => {
const responseOptions = new ResponseOptions({
body: JSON.stringify(/* some response */)
});
mockConnection.mockRespond(new Response(responseOptions));
});
service.getSomeObservable()
.subscribe(result => {
expect(result).toBe(/* expected response */);
});
}));
Do I need to unsubscribe from the subscription in an afterEach
or afterAll
block to avoid memory issues? Or will it be automatically removed?
@estus is correct, you do not need to unsubscribe.
tl;dr;
Http Observables do complete, disgarding subscriptions at that time, and freeing them up for GC.
Until the Observable/Subject gets torn down or completes, no subscription can be GC'd unless you unsubscribe.
Consider this simple Angular TestBed setup:
In the case of an
Http
request via the AngularHttpClient
, the Observable will complete, and the references to Subscribers will be released for GC. In the case of general Angular Jasmine Testing as shown above--even if you had an Observable that didn't complete--theDataService
object being referenced byservice
gets reset every timebeforeEach
is called, and the oldDataService
object will have lost all references and is marked for GC. Once that happens, any subscriptions referenced by the Observable/Subject in theDataService
will lose references to them, and they'll be GC'd as well.Remember, memory leaks happen with Observables because they retain references to all Subscribers and unless the Subscriber unsubscribes. They will continue to have valid references to them and their containing functions, and cannot be marked for GC until The Subject/Observable is destroyed. If the Subject continues to live and you keep adding subscribers--such as new instance of the same Subscriber--you will keep adding a bunch of old, unused objects to memory.