美文网首页
LeakCanary内存泄漏优化

LeakCanary内存泄漏优化

作者: KennGM | 来源:发表于2022-07-13 17:49 被阅读0次

    接手了一个屎坑代码 在有限的改动下修改泄漏问题
    以下是我收集的一下泄漏问题及修改办法
    常见的内存泄漏主要有以下4种
    1.“静态实例” 造成内存泄漏

    ┬───                                                                                                     
    │ GC Root: System class                                                                                  
    │                                                                                                        
    ├─ android.net.ConnectivityManager class                                                                 
    │    Leaking: NO (a class is never leaking)                                                              
    │    ↓ static ConnectivityManager.sInstance                                                              
    │                                 ~~~~~~~~~                                                              
    ├─ android.net.ConnectivityManager instance                                                              
    │    Leaking: UNKNOWN                                                                                    
    │    Retaining 114 B in 5 objects                                                                        
    │    mContext instance of com.MainActivity with mDestroyed = true                      
    │    ↓ ConnectivityManager.mContext                                                                      
    │                          ~~~~~~~~                                                                      
    ╰→ com.MainActivity instance                                                           
         Leaking: YES (ObjectWatcher was watching this because com.MainActivity received  
         Activity#onDestroy() callback and Activity#mDestroyed is true)                                     
         Retaining 27.8 MB in 17166 objects                                                                 
         key = ecff8e2d-70c4-4cbf-99f1-d02005ddd4f2                                                         
         watchDurationMillis = 7209                                                                         
         retainedDurationMillis = 2207                                                                      
         mApplication instance of com.Application                                   
         mBase instance of androidx.appcompat.view.ContextThemeWrapper                                      
    
    实际代码和修改办法

    这种的改法其实和很简单只要getApplicationContext即可
    原因你这个方法都是静态的如果你传入的Context是非Application的那么它将会因为这个静态而不会释放 所以只要改成Application Context即可

    另外一种DialogFragment引起的泄漏


    DialogFragment泄漏

    这种就极其脑抽 解决也很简单 首先DialogFragment这玩意是个Fragment 也就是说他有自己的Context


    写法和改法

    正常是Dialog里面持有外部的Context 比如 有个静态的Context常量 这是我们正常的理解
    但是这东西就反过来这是外部持有了另外一个伪装的Fragment 所以解决办法就是给他搞个弱应用或者匿名对象

    GC Root: Input or output parameters in native code 
    
    ├─ dalvik.system.PathClassLoader instance
     │    Leaking: NO (m↓ is not leaking and A ClassLoader is never leaking)
     │    ↓ ClassLoader.runtimeInternalObjects
    
     ┬───                                                                                                                          
     │ GC Root: Global variable in native code                                                                                     
     │                                                                                                                             
     ├─ dalvik.system.PathClassLoader instance                                                                                     
     │    Leaking: NO (IntentUtil↓ is not leaking and A ClassLoader is never leaking)                                              
     │    ↓ ClassLoader.runtimeInternalObjects                                                                                     
     ├─ java.lang.Object[] array                                                                                                   
     │    Leaking: NO (IntentUtil↓ is not leaking)                                                                                 
     │    ↓ Object[4169]                                                                                                           
     ├─ com.IntentUtil class                                                                          
     │    Leaking: NO (a class is never leaking)                                                                                   
     │    ↓ static IntentUtil.context                                                                                              
     │                        ~~~~~~~                                                                                              
     ╰→ com.MineMdActivity instance                                                  
          Leaking: YES (ObjectWatcher was watching this because com.miaodou.                
          MineMdActivity received Activity#onDestroy() callback and Activity#mDestroyed is true)                                  
          Retaining 9.7 MB in 2450 objects                                                                                        
          key = 366658d5-9533-4aa5-85ed-1397537a9089                                                                              
          watchDurationMillis = 31774                                                                                             
          retainedDurationMillis = 26773                                                                                          
          mContractView instance of com.miaodou.MineMdActivity with mDestroyed = true       
          context instance of com.MineMdActivity with mDestroyed = true             
          mApplication instance of com.sbds.secondvoice.MiaoyinApplication                                                        
          mBase instance of androidx.appcompat.view.ContextThemeWrapper                                                           
                                                                                                                                   
    
    这就是大聪明

    为了静态初始化 为了个单例 写成这样 另外我也实在不懂一个Intent也要写个单例工具类干嘛 警告都不看
    很多程序员有一个坏习惯 警告都觉得无所谓 实际上很多问题很难解决找不到原因 就是警告没解决
    解决办法也简单 不要单例 不要静态即可

    ┬───                                                                                                                    
    │ GC Root: Thread object                                                                                                
    │                                                                                                                       
    ├─ java.lang.Thread instance                                                                                            
    │    Leaking: NO (the main thread always runs)                                                                          
    │    Thread name: 'main'                                                                                                
    │    ↓ Thread.threadLocals                                                                                              
    │             ~~~~~~~~~~~~                                                                                              
    ├─ java.lang.ThreadLocal$ThreadLocalMap instance                                                                        
    │    Leaking: UNKNOWN                                                                                                   
    │    Retaining 7.4 kB in 74 objects                                                                                     
    │    ↓ ThreadLocal$ThreadLocalMap.table                                                                                 
    │                                 ~~~~~                                                                                 
    ├─ java.lang.ThreadLocal$ThreadLocalMap$Entry[] array                                                                   
    │    Leaking: UNKNOWN                                                                                                   
    │    Retaining 7.3 kB in 73 objects                                                                                     
    │    ↓ ThreadLocal$ThreadLocalMap$Entry[18]                                                                             
    │                                      ~~~~                                                                             
    ├─ java.lang.ThreadLocal$ThreadLocalMap$Entry instance                                                                  
    │    Leaking: UNKNOWN                                                                                                   
    │    Retaining 28 B in 1 objects                                                                                        
    │    ↓ ThreadLocal$ThreadLocalMap$Entry.value                                                                           
    │                                       ~~~~~                                                                           
    ├─ android.animation.AnimationHandler instance                                                                          
    │    Leaking: UNKNOWN                                                                                                   
    │    Retaining 5.0 MB in 7251 objects                                                                                   
    │    ↓ AnimationHandler.mAnimationCallbacks                                                                             
    │                       ~~~~~~~~~~~~~~~~~~~                                                                             
    ├─ java.util.ArrayList instance                                                                                         
    │    Leaking: UNKNOWN                                                                                                   
    │    Retaining 5.0 MB in 7247 objects                                                                                   
    │    ↓ ArrayList[0]                                                                                                     
    │               ~~~                                                                                                     
    ├─ android.animation.ObjectAnimator instance                                                                            
    │    Leaking: UNKNOWN                                                                                                   
    │    Retaining 5.0 MB in 7245 objects                                                                                   
    │    mListeners = null                                                                                                  
    │    mPropertyName = rotation                                                                                           
    │    mProperty = null                                                                                                   
    │    mInitialized = true                                                                                                
    │    mStarted = true                                                                                                    
    │    mRunning = true                                                                                                    
    │    mAnimationEndRequested = false                                                                                     
    │    mDuration = 3000                                                                                                   
    │    mStartDelay = 0                                                                                                    
    │    mRepeatCount = INFINITE (-1)                                                                                       
    │    mRepeatMode = RESTART (1)                                                                                          
    │    ↓ ObjectAnimator.mTarget                                                                                           
    │                     ~~~~~~~                                                                                           
    ├─ androidx.appcompat.widget.AppCompatImageView instance                                                                
    │    Leaking: YES (View.mContext references a destroyed activity)                                                       
    │    Retaining 5.0 MB in 7231 objects                                                                                   
    │    View not part of a window view hierarchy                                                                           
    │    View.mAttachInfo is null (view detached)                                                                           
    │    View.mID = R.id.iv_record                                                                                          
    │    View.mWindowAttachCount = 0                                                                                        
    │    mContext instance of com.ShortVideoMainActivity with mDestroyed = true             
    │    ↓ View.mContext                                                                                                    
    ╰→ com.ShortVideoMainActivity instance                                                  
         Leaking: YES (ObjectWatcher was watching this because com.ShortVideoMainActivity  
         received Activity#onDestroy() callback and Activity#mDestroyed is true)                                           
         Retaining 64.8 kB in 1152 objects                                                                                 
         key = e14565c2-166a-4e0a-9dd3-07f981f9f18d                                                                        
         watchDurationMillis = 5820                                                                                        
         retainedDurationMillis = 819                                                                                      
         context instance of com.ShortVideoMainActivity with mDestroyed = true             
         mApplication instance of com.Application                                                  
         mBase instance of androidx.appcompat.view.ContextThemeWrapper                                                     
    

    查找问题发生位置

    我们就能很快速的找到那个类 根据 View.mID = R.id.iv_record 知道那个控件泄漏并定位到代码

            rotation = ObjectAnimator.ofFloat(ivRecord, "rotation", 0, 360f);
            rotation.setRepeatCount(ValueAnimator.INFINITE);
            rotation.setRepeatMode(ValueAnimator.RESTART);
            rotation.setInterpolator(new LinearInterpolator());
            rotation.setDuration(3000).start();
    

    解决方案 :

    然后看代码 这个ObjectAnimator是一直在播放所以我们应该在页面销毁的时候cancel掉他
    但是好巧不巧这玩意是在adapter里面的一个自定义view 那么怎么释放呢
    应该这样重写onDetachedFromWindow方法然后释放

        @Override
        protected void onDetachedFromWindow() {
            rotation.cancel();
            super.onDetachedFromWindow();
        }
    

    相关文章

      网友评论

          本文标题:LeakCanary内存泄漏优化

          本文链接:https://www.haomeiwen.com/subject/vzwrprtx.html