leakcanary
leakcanary copied to clipboard
Memory leak on HostApduService
I see cumulative memory leak when app is communicating with hardware reader via NFC. I see this leak when app is running on foreground and background. I verified this leak using Leak Canary lib. I see the issue on Android 11 and 12, both are [Samsung.]
LeakTrace information
┬───
│ GC Root: Global variable in native code
│
├─ android.os.Handler$MessengerImpl instance
│ Leaking: UNKNOWN
│ Retaining 2.0 kB in 12 objects
│ ↓ Handler$MessengerImpl.this$0
│ ~~~~~~
├─ android.nfc.cardemulation.HostApduService$MsgHandler instance
│ Leaking: UNKNOWN
│ Retaining 1.4 kB in 11 objects
│ this$0 instance of com.example.myapplication.MyHostApduService
│ ↓ HostApduService$MsgHandler.this$0
│ ~~~~~~
╰→ com.example.myapplication.MyHostApduService instance
Leaking: YES (ObjectWatcher was watching this because com.example.myapplication.MyHostApduService received
Service#onDestroy() callback and Service not held by ActivityThread)
Retaining 1.4 kB in 10 objects
key = 973e5c36-af36-44a5-9f2e-9304be9a989b
watchDurationMillis = 5178
retainedDurationMillis = 174
mApplication instance of android.app.Application
mBase instance of android.app.ContextImpl
METADATA
Build.VERSION.SDK_INT: 29
Build.MANUFACTURER: samsung
LeakCanary version: 2.9.1
App process name: com.example.myapplication
Class count: 12221
Instance count: 115035
Primitive array count: 83235
Object array count: 15185
Thread count: 22
Heap total bytes: 14196604
Bitmap count: 5
Bitmap total bytes: 79173
Large bitmap count: 0
Large bitmap total bytes: 0
Db 1: open /data/user/0/com.example.myapplication/databases/leaks.db
Stats: LruCache[maxSize=3000,hits=30041,misses=74443,hitRate=28%]
RandomAccess[bytes=3534436,reads=74443,travel=19649182613,range=17538120,size=22508266]
Analysis duration: 26083 ms
This looks like a proper leak in the Android Framework itself. Can you file an issue on the official Android bug tracker?
HostApduService.MsgHandler is a non static inner handler class that keeps a ref to its outer class (a service) as long as the handler is alive.
https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/nfc/cardemulation/HostApduService.java;l=245;drc=a8cec413b65bfc3ea23575b34eb983c20944491b
Could you repro with API 33 and then file a bug on the Google bug tracker?
Going forward this leak will be reported by LeakCanary as a known library leak.