SimpleBluetoothTerminal icon indicating copy to clipboard operation
SimpleBluetoothTerminal copied to clipboard

Memory Leaks at SerialBinder

Open talhacomak opened this issue 2 years ago • 2 comments

I am getting memory leak error at SerialBinder instance in Serial Service. I converted TerminalFragment into an Activity and i inserted bindService function to onCreate() and unbindService function to end of the onDestroy() methods. Where could the problem be and any suggestion for solutions? Thanks for advance.

talhacomak avatar May 24 '22 06:05 talhacomak

i also use startService and stopService and keep a reference to the service in onServiceConnected. The fragment uses setRetainInstance() to only get a single instance created and the service reference is still valid after rotation. you should check if service reference is still valid after rotating your Android device

kai-morich avatar May 24 '22 06:05 kai-morich

The memory leak is occuring after onDestroy method of the Activity. The Leak Canary Logs:

┬───
│ GC Root: Global variable in native code
│
├─ mpackage.bluetooth.SerialService$SerialBinder instance
│    Leaking: UNKNOWN
│    Retaining 28 B in 1 objects
│    this$0 instance of mpackage.SerialService
│    ↓ SerialService$SerialBinder.this$0
│                                 ~~~~~~
╰→ mpackage.SerialService instance
​     Leaking: YES (ObjectWatcher was watching this because mpackage.bluetooth.SerialService received Service#onDestroy()
​     callback and Service not held by ActivityThread)
​     Retaining 693 B in 11 objects
​     key = 5980adb2-c359-4363-9726-13d02d33777b
​     watchDurationMillis = 6755
​     retainedDurationMillis = 1752
​     mApplication instance of mpackage.BaseApp
​     mBase instance of android.app.ContextImpl

METADATA

Build.VERSION.SDK_INT: 27
Build.MANUFACTURER: AGM
LeakCanary version: 2.9.1
App process name: mpackage
Class count: 6755
Instance count: 115426
Primitive array count: 71206
Object array count: 10276
Thread count: 30
Heap total bytes: 10624694
Bitmap count: 170
Bitmap total bytes: 293038536
Large bitmap count: 4
Large bitmap total bytes: 148390448
Db 1: closed /data/user/0/mpackage/databases/frnsc.s3db
Db 2: closed /data/user/0/mpackage/databases/frnsc.s3db
Db 3: closed /data/user/0/mpackage/databases/frnsc.s3db
Stats: LruCache[maxSize=3000,hits=32975,misses=68391,hitRate=32%]
RandomAccess[bytes=3762398,reads=68391,travel=15029771222,range=13539355,size=16
550259]
Analysis duration: 18288 ms```

talhacomak avatar May 24 '22 06:05 talhacomak