LC is a very useful tool to prevent memory leaks, but LC comes with monitoring that only supports Activity, Fragment, ViewModel, RootView, and Service objects. If we want to monitor more objects, we can only call its interface to manually add monitoring. It is still inconvenient to use, so I try to use bytecode instrumentation to automatically monitor more objects.
By bytecode, the following are supported:
java.io.Closeable#close
- file stream
- Socket
- Cursor
- Other inherits from Closeable
android.graphics.Bitmap#recycle
Insertion tool: https://github.com/miqt/android-plugin/tree/master/plugin/inject-code-plugin
Addendum:
https://github.com/square/leakcanary/blob/main/leakcanary-object-watcher-android-core/src/main/java/leakcanary/AppWatcher.kt