Giter VIP home page Giter VIP logo

rx-disposable-watcher's Introduction

RxDisposableWatcher โ€” find leaked subscriptions in RxJava code ๐Ÿž

Download Android Arsenal

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 subscribed to Thermometer instance but never released a Disposable resource later. As a result it can blow up an application logic or even cause a memory leak! ๐Ÿ’ฉ

๐Ÿ”ฅ Read my post on Medium: Detect Leaked Subscriptions in RxJava code with RxDisposableWatcher ๐Ÿ”ฅ

Use RxDisposableWatcher plugin to find all undestroyed subscriptions & build the detailed HTML report:

Everything we need: stack trace, number of calls & Observable types.

Getting started

Setup

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()

For Android application add storage permission into AndroidManifest.xml to save & pull generated HTML report:

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

โš ๏ธ Note: in case you're facing with IllegalStateException: Plugins can't be changed anymore, then another application component tries to use RxJavaPlugins utility class with exclusive access. Disable this component when working with the plugin.

Make snapshot & generate HTML report

Now you're ready to go! Check whether you have alive Rx subscriptions at the moment:

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

For Android save the report to SD card:

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

Display HTML report in a desktop browser

Pull report file from Android device and display (replace 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!

Displaying HTML report in one click (Like a boss) ๐Ÿ˜Ž

I want a magic button in Android Studio toolbar to show HTML report in one click!

The idea is pretty simple:

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

How to add this button? ๐Ÿ‘‰ Read the dedicated section in my post: Get a report in one click from Android Studio

Licence

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.

rx-disposable-watcher's People

Contributors

andreyfomenkov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.