Debugging memory leaks
In debug mode Rx tracks all allocated resources in a global variable Resources.total
.
In case you want to have some resource leak detection logic, the simplest method is just printing out RxSwift.Resources.total
periodically to output.
/* add somewhere in
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil)
*/
_ = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
.subscribe(onNext: { _ in
print("Resource count \(RxSwift.Resources.total)")
})
Most efficient way to test for memory leaks is:
- navigate to your screen and use it
- navigate back
- observe initial resource count
- navigate second time to your screen and use it
- navigate back
- observe final resource count
In case there is a difference in resource count between initial and final resource counts, there might be a memory leak somewhere.
The reason why 2 navigations are suggested is because first navigation forces loading of lazy resources.