report
report


RxDisposableWatcher – Find leaked subscriptions in the RxJava code 🐞

The problem

Consider the following RxJava code:

class Thermometer { fun observeTemperature(): Observable<Int>
}// ...val thermometer = Thermometer.getInstance()// ...
thermometer .observeTemperature() .subscribe { /* ... */ } // Subscribed, but not disposed afterwards!

We have subscribed Thermometer Instance, but never released a single-use resource later. As a result, it can blow up application logic or even cause a memory leak! ????

🔥 Read my post on Medium: Detecting Leaked Subscriptions in RxJava Code Using RxDisposableWatcher 🔥

Use the RxDisposableWatcher plugin to find all subscriptions that have not been destroyed and generate the detailed HTML report:

All we need: Stack trace, number of calls and observable types.

Started

To install

Gradle:

repositories { jcenter()
}
implementation 'ru.fomenkov:rx-disposable-watcher:x.y.z'

Maven:

<dependency> <groupId>ru.fomenkov</groupId> <artifactId>rx-disposable-watcher</artifactId> <version>x.y.z</version> <type>pom</type>
</dependency>

Please replace x.y.z with the latest version numbers:

initialization

RxDisposableWatcher.init()

Add storage permissions for Android applications AndroidManifest.xml To save and drag generated HTML reports:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

⚠️Note: in case you are faced with IllegalStateException: Plugins can't be changed anymoreAnother application component then tries to use the RxJavaPlugins utility class with exclusive access. Deactivate this component when you are working with the plugin.

Take a snapshot and generate an HTML report

Now you are ready to go! Check if you currently have Rx subscriptions:

val result = RxDisposableWatcher.probe() // Collect info: stacktrace, number of calls, typeval report = HtmlReportBuilder(result).build() // Generate HTML report

For Android, save the report on the SD card:

val report = ...val file = File(context.getExternalFilesDir(null), "report.html") // Specify filenameval stream = FileOutputStream(file)
stream.use { it.write(report.toByteArray()) }

View the HTML report in a desktop browser

Drag and view the report file from the Android device (replace it with your paths):

adb pull /sdcard/report.html ~/report.html # Grab a report from SD card
open ~/report.html # for Mac# or
google-chrome ~/report.html # for Linux

That’s it!

View HTML report with one click (like a boss) 😎

I want a magic button in the Android Studio toolbar to view HTML reports with one click!

The idea is pretty simple:

As a lazy developer, I prefer the approach described because it saves my time dramatically!

How do I add this button? 👉 Check out the relevant section in my post: Get a Report from Android Studio in One Click

License

Copyright 2020 Andrey Fomenkov
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

LEAVE A REPLY

Please enter your comment!
Please enter your name here