RxAndroidBle: scanBleDevices causes memory leak
Summary
Doing a basic scan of ble devices causes a memory leak despite ending the scan subscription at onStop()
.
Library version
1.10.1
Preconditions
Have sample app cloned & loaded: https://github.com/seljabali/rxandroidble2-leak
Steps to reproduce actual result
1. Enable Bluetooth on device
2. Enable GPS
3. Open app
4. Hit back button
5. Pull system drop down
6. Wait for Canary to analyze heap
7. Canary momentarily shows leak.
Actual result
- Canary showing a memory leak
Expected result
- Canary not showing a memory leak
Minimum code snippet reproducing the issue
scanDisposable = RxBle.get()
.scanBleDevices(
ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.build()
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError {
onScanError() // commenting this out stops leak
}
.doOnNext {
}
.subscribe()
Logs from the application running with settings:
https://github.com/seljabali/rxandroidble2-leak/blob/master/leakLog.txt
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 18 (11 by maintainers)
This looks exactly as before, starting the leak at Android’s
BleScanCallbackWrapper
. I will try to run it once I will get my hands on an API 29 device.Hello, I’ve created a PR with a solution to the issue
https://github.com/Polidea/RxAndroidBle/pull/708
Hope it can be merged soon
Great investigative efforts @dariuszseweryn! Thank you. I’ll create a Google Issue accordingly, and post it back here as a comment – closing ticket.
I have modified the example so it does not use
RxAndroidBle
at all and run the vanilla API. Changes looks like this:LeakCanary result:
I think that this issue should be checked against the newest Android OS version and reported on Google Issue Tracker
I have looked through the classes that are referenced by the trace but all of those should be garbage collected normally with their
Observable
chains. I do not see anything leaking in eitherScanOperationApi21
(which is held by the OS) nor inFIFORunnableEntry
which is held because the operation is not collected.Correct me if I am wrong but it seems that the root-cause of the leak is
Which is a part of Android OS — not library’s?