AndroidUtilCode icon indicating copy to clipboard operation
AndroidUtilCode copied to clipboard

PermissionUtils 内存泄漏

Open blisssayyid opened this issue 2 years ago • 1 comments

描述 Bug

使用工具类时发生内存泄漏

  • AndroidUtilCode 的版本:utilcodex:1.31.1
  • 出现 Bug 的设备型号:Pixel5
  • 设备的 Android 版本:API 31

相关代码

 /**
     * 快速请求权限
     */
    fun request(
        onAllGranted: () -> Unit,
        @PermissionConstants.PermissionGroup vararg permissions: String,
        onDenied: () -> Unit = { ToastUtils.showLong("未开启相关权限") },
    ) {
        PermissionUtils.permission(*permissions)
            .rationale { _, shouldRequest -> showRationaleDialog(mContext, shouldRequest, mLifecycle) }
            .callback(object : PermissionUtils.SingleCallback {
                override fun callback(
                    isAllGranted: Boolean,
                    granted: MutableList<String>,
                    deniedForever: MutableList<String>,
                    denied: MutableList<String>
                ) {
                    if (isAllGranted) {
                        // 全部通过
                        onAllGranted()
                        return
                    }
                    if (deniedForever.isNotEmpty()) {
                        // 有永久拒绝的权限
                        showOpenAppSettingDialog(mContext, mLifecycle)
                        return
                    }
                    // 没有全部通过
                    onDenied()
                }
            })
            .request()
    }

异常堆栈

  ====================================
                                                                                                    HEAP ANALYSIS RESULT
                                                                                                    ====================================
                                                                                                    1 APPLICATION LEAKS
                                                                                                    
                                                                                                    References underlined with "~~~" are likely causes.
                                                                                                    Learn more at https://squ.re/leaks.
                                                                                                    
                                                                                                    199775 bytes retained by leaking objects
                                                                                                    Displaying only 1 leak trace out of 5 with the same signature
                                                                                                    Signature: b7c81aff62fcf437fefb4ace2f786ab2fa9ed657
                                                                                                    ┬───
                                                                                                    │ GC Root: Global variable in native code
                                                                                                    │
                                                                                                    ├─ android.app.AppOpsManager$3 instance
                                                                                                    │    Leaking: UNKNOWN
                                                                                                    │    Retaining 536 B in 1 objects
                                                                                                    │    Anonymous subclass of com.android.internal.app.IAppOpsActiveCallback$Stub
                                                                                                    │    ↓ AppOpsManager$3.val$callback
                                                                                                    │                      ~~~~~~~~~~~~
                                                                                                    ├─ android.permission.PermissionUsageHelper instance
                                                                                                    │    Leaking: UNKNOWN
                                                                                                    │    Retaining 126 B in 5 objects
                                                                                                    │    mContext instance of com.blankj.utilcode.util.UtilsTransActivity with mDestroyed = true
                                                                                                    │    ↓ PermissionUsageHelper.mContext
                                                                                                    │                            ~~~~~~~~
                                                                                                    ╰→ com.blankj.utilcode.util.UtilsTransActivity instance
                                                                                                    ​     Leaking: YES (ObjectWatcher was watching this because com.blankj.utilcode.util.UtilsTransActivity received
                                                                                                    ​     Activity#onDestroy() callback and Activity#mDestroyed is true)
                                                                                                    ​     Retaining 40.0 kB in 833 objects
                                                                                                    ​     key = 5ee3e8ef-f9dd-418d-83ea-cadb4a2a98ac
                                                                                                    ​     watchDurationMillis = 5924
                                                                                                    ​     retainedDurationMillis = 923
                                                                                                    ​     mApplication instance of com.xxx.bedrock.app.AppApplication
                                                                                                    ​     mBase instance of androidx.appcompat.view.ContextThemeWrapper
                                                                                                    ====================================
                                                                                                    0 LIBRARY LEAKS
                                                                                                    
                                                                                                    A Library Leak is a leak caused by a known bug in 3rd party code that you do not have control over.
                                                                                                    See https://square.github.io/leakcanary/fundamentals-how-leakcanary-works/#4-categorizing-leaks
                                                                                                    ====================================
                                                                                                    0 UNREACHABLE OBJECTS
                                                                                                    
                                                                                                    An unreachable object is still in memory but LeakCanary could not find a strong reference path
                                                                                                    from GC roots.
                                                                                                    ====================================
                                                                                                    METADATA
                                                                                                    
                                                                                                    Please include this in bug reports and Stack Overflow questions.
                                                                                                    
                                                                                                    Build.VERSION.SDK_INT: 31
                                                                                                    Build.MANUFACTURER: unknown
                                                                                                    LeakCanary version: 2.9.1
                                                                                                    App process name: com.xxx.basegis
                                                                                                    Class count: 28040
                                                                                                    Instance count: 242816
                                                                                                    Primitive array count: 157968
                                                                                                    Object array count: 30981
                                                                                                    Thread count: 46
                                                                                                    Heap total bytes: 36150526
                                                                                                    Bitmap count: 23
                                                                                                    Bitmap total bytes: 1030595
                                                                                                    Large bitmap count: 0
                                                                                                    Large bitmap total bytes: 0
                                                                                                    Stats: LruCache[maxSize=3000,hits=120915,misses=210835,hitRate=36%]
                                                                                                    RandomAccess[bytes=10498560,reads=210835,travel=107894972371,range=38766153,size=50721662]
                                                                                                    Heap dump reason: 8 retained objects, app is visible
                                                                                                    Analysis duration: 5911 ms
                                                                                                    Heap dump file path: /storage/emulated/0/Download/leakcanary-com.xxx.basegis/2022-10-24_08-03-18_533.hprof
                                                                                                    Heap dump timestamp: 1666598612228
                                                                                                    Heap dump duration: 1591 ms
                                                                                                    ====================================

截图

无。

blisssayyid avatar Oct 24 '22 08:10 blisssayyid

这个是这个版本的android系统内存泄露呀, 跟库没得关系 close掉吧小伙子, 如果有兴趣可以看看轮子哥规避这个泄露的库/文章

liangrk avatar Nov 10 '22 14:11 liangrk