I'm watching this presentation, and at 13:42 they say that using lambda in the way:
api.getEvents()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(() -> loadingIndication.show())
.doOnUnsubscribe(() -> loadingIndication.hide())
.subscribe(...);
causes View to leak.
Can you explain how the leak works in this case? Does the leak appear depending on how we compile the code and what class we put the RxJava code (e.g. in Activity, in Application, in Service)?
Memory leaks occur when an application allocates memory for an object, but then fails to release the memory when the object is no longer being used. Over time, leaked memory accumulates and results in poor app performance and even crashes.
Make sure to call free() when you use malloc() or calloc() . Don't reassign pointer which points to allocated memory location without free() ing it first i.e. don't lose the reference.
Option 1: As mentioned before never create a static variable of an inner class. Option 2: The class should be set to static. Instances of anonymous classes do not hold an implicit reference to their outer class when they are “static”. Option 3: Use a weakReference of any view/activity.
This causes a leak because lambdas are no different from anonymous inner classes where they will have an implicit reference to the current class where they're called, in this case it the Activity. So this piece of code actually has reference to your Activity.
api.getEvents()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(() -> loadingIndication.show())
.doOnUnsubscribe(() -> loadingIndication.hide())
.subscribe(events -> {}, throwable -> {});
I haven't had the time to checkout the video but there is a way to handle this kind of possible memory leak by using CompositeDisposable and adding the Disposable
returned from the above code through compositeDisposable.add()
and calling compositeDisposable.clear()
on your Activity's onDestroy()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With