在使用高德地图的时候,喜提了一个内存泄露:
┬───
│ GC Root: Global variable in native code
│
├─ android.location.LocationManager$GnssStatusListenerTransport instance
│ Leaking: UNKNOWN
│ Retaining 588 B in 3 objects
│ ↓ LocationManager$GnssStatusListenerTransport.mGnssCallback
│ ~~~~~~~~~~~~~
├─ com.loc.h$1 instance
│ Leaking: UNKNOWN
│ Retaining 12 B in 1 objects
│ Anonymous subclass of android.location.GnssStatus$Callback
│ ↓ h$1.a
│ ~
├─ com.loc.h instance
│ Leaking: UNKNOWN
│ Retaining 197 B in 4 objects
│ z instance of com.lu.code.main.DetailConfigActivity with mDestroyed
│ = true
│ ↓ h.z
│ ~
╰→ com.lu.code.main.DetailConfigActivity instance
? Leaking: YES (ObjectWatcher was watching this because com.lu.code.
? main.DetailConfigActivity received Activity
? Activity
? Retaining 694.8 kB in 7703 objects
? key = 8e2c9606-0df8-4e92-9e21-0d4d3d5ca64e
? watchDurationMillis = 5660
? retainedDurationMillis = 618
? mApplication instance of com.lu.code.App
? mBase instance of androidx.appcompat.view.ContextThemeWrapper
METADATA
Build.VERSION.SDK_INT: 29
Build.MANUFACTURER: Xiaomi
LeakCanary version: 2.8.1
App process name: com.lu.code
Count of retained yet cleared: 3 KeyedWeakReference instances
Stats: LruCache[maxSize=3000,hits=50801,misses=111621,hitRate=31%]
RandomAccess[bytes=6638804,reads=111621,travel=40796300256,range=26287634,size=3
0953215]
Analysis duration: 9785 ms
兄弟们仔细看,杂么漏着漏着,到FrameWork层了,奇葩。android.location.LocationManager$GnssStatusListenerTransport instance
但其实,这个
AMapLocationClient.updatePrivacyShow(context, true, true);
AMapLocationClient.updatePrivacyAgree(context, true);
mLocationClient = new AMapLocationClient(context);
mLocationClientOption = new AMapLocationClientOption();
mLocationClient = new AMapLocationClient(context); 问题出在这儿,传了Activity当作context,导致的泄露。改为ApplicationContext 即可规避
|