RxSwift Memory Management

I will show the example first:

In this example, we have an Observable emitting events every 300 milliseconds and a Disposable that is returning as the result of subscribe function.

Here, although our view controller does not keep a strong reference for this subscription, the console keeps printing events:

The reason why the subscription kept in memory is because RxSwift creates a strong reference cycle between Observable and Disposable. They refer to each other and subscription stays alive.

That’s why we often use disposeBag to dispose the subscription when our UIViewController gets deallocated.

--

--