Do I need to unsubscribe from subscriptions in my

2020-07-03 07:48发布

问题:

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?

回答1:

@estus is correct, you do not need to unsubscribe.

tl;dr;

  • Observables/Subjects hold refererences to all subscribers until complete.
  • Objects with active references cannot be marked for Garbage collection. Basic JS.
  • 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:

describe(`DataService`, () => {
  let service: DataService;
  let httpMock: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [
        DataService,
      ]
    });

    service = TestBed.get(DataService);
    httpMock = TestBed.get(HttpTestingController);
  });

  it(`should get a response`, () => {
    service.getData().subscribe(response => {
      expect(response).toBeTruthy();
    });
  });

In the case of an Http request via the Angular HttpClient, 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--the DataService object being referenced by service gets reset every time beforeEach is called, and the old DataService object will have lost all references and is marked for GC. Once that happens, any subscriptions referenced by the Observable/Subject in the DataService 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.