Detect Leaked Subscriptions in RxJava code using RxDisposableWatcher
Sometimes it’s possible to forget about releasing a Disposable resource in RxJava code. Consider the following code snippet:
As mentioned in the comment, the subscription has not been destroyed later. Of course this is legal only in case it was done intentionally and consciously.
Otherwise it can blow up application logic or cause a memory leak! 💩
Discover leaks with RxDisposableWatcher 🐞
RxDisposableWatcher is a debug tool for monitoring not destroyed Disposable subscriptions in RxJava code. The output exhaustive report in HTML format:
The report shows up a list of all Rx subscriptions currently alive in application. For example, if during
Activity destroy some of them are not being released as expected, then probably it’s a bug. The plugin will help to deal with them.
At first please visit the project on GitHub for more information:
Monitoring leaked Disposable subscriptions in RxJava code
Plugin usage is very simple (example for Android):
report string value to HTML file somewhere on Android SD card, pull to the desktop and finally display in a browser. 🔥 That’s it! 🔥
Typical usage example
Let’s find a leak in the following weather application on Android:
WeatherActivity and back 7 times, then build a report. All records are sorted by calls number. One item is suspicious:
Even though the
WeatherActivity has been finished, we are still observing ℃ value from
Thermometer instance! The report says that subscription has been made in
WeatherActivity at line 9.
Consider CompositeDisposable for releasing multiple Disposables at once.
Get a report in one click from Android Studio 😳
I want a magic button in Android Studio to display HTML report in one click. Well, the idea is pretty simple:
It consists of three components:
 Shell script to send broadcast event, pull report and display in a browser;
 BroadcastReceiver event handler in Android application;
 The magic button itself.
[ 1 ] Shell script
Download the script here. Also make it executable with
chmod +x report.sh
[ 2 ] BroadcastReceiver event handler
onCreate() of your
[ 3 ] Adding a magic button to Android Studio toolbar
First of all create custom action in Android Studio with External Tools:
Then add a new button. Right-click on the main toolbar → Customize Menus and Toolbars… → Main Toolbar section. Choose action and icon for button:
Now after all proper configurations click on the magic button and you’ll see the generated HTML report in a desktop browser 🥳🎉
In case of question you can get in touch with me on Twitter.
RxDisposableWatcher is yet another debugging tool to make your project bug free.
Have a nice day!