美文网首页
android内存泄漏常见情况以及解决方法

android内存泄漏常见情况以及解决方法

作者: null_js | 来源:发表于2019-01-30 14:20 被阅读7次

    一、Fragment中使用RecyclerView:

    解决方法如下:

       @Override
        public void onDestroyView() {
            super.onDestroyView();
            mRecyclerView = null;
            mSwipeRefreshLayout = null;
            mAdapter = null;
            notDataView = null;
            errorView = null;
        }
    

    二、单例中接口:

    不用的时候要及时设为null:

        @Override
        public void onDestroy() {
            super.onDestroy();
            DropManager.getInstance().setDropListener(null);
        }
    

    三、Timer和TimerTask:

    不用的时候要及时cancel()掉:

         /**
         * 用完cancel掉
         */
        private void cancelTimer() {
            if (mTimer != null) {
                mTimer.cancel();
                mTimer = null;
            }
            if (timerTask != null) {
                timerTask.cancel();
                timerTask = null;
            }
        }
    

    四、属性动画:

    不用的时候要及时cancel()掉:

         if (mUpAnimatorSet != null) {
                if (mUpAnimatorSet.isRunning()) {
                    mUpAnimatorSet.cancel();
                }
                mUpAnimatorSet.removeAllListeners();
                for (Animator animator : mUpAnimatorSet.getChildAnimations()) {
                    animator.removeAllListeners();
                }
                mUpAnimatorSet = null;
            }
    

    五、EditText造成的内存泄漏:

    泄漏日志:

    02-01 10:53:54.214 16435-18974/com.jinnuojiayin.haoshengshuohua:leakcanary D/LeakCanary: * ↳ InputConnectionWrapper.!(mTarget)!
    02-01 10:53:54.214 16435-18974/com.jinnuojiayin.haoshengshuohua:leakcanary D/LeakCanary: * ↳ EditableInputConnection.!(mTextView)!
    02-01 10:53:54.214 16435-18974/com.jinnuojiayin.haoshengshuohua:leakcanary D/LeakCanary: * ↳ AppCompatEditText.mContext
    02-01 10:53:54.214 16435-18974/com.jinnuojiayin.haoshengshuohua:leakcanary D/LeakCanary: * ↳ TeamMessageActivity
    

    解决方法:

        @Override
        protected void onDestroy() {
            super.onDestroy();
            mEtSearch.removeTextChangedListener(mTextChangedListener);
            mTextChangedListener = null;
            mEtSearch.setOnEditorActionListener(null);
        }
    
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.EditText;
    
    import java.lang.reflect.Field;
    
    /**
     * Created by xyl on 2019/2/1.
     * 防止出现内存泄漏
     */
    @SuppressLint("AppCompatCustomView")
    public class BaseEditText extends EditText {
        private static Field mParent;
    
        static {
            try {
                mParent = View.class.getDeclaredField("mParent");
                mParent.setAccessible(true);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
    
        public BaseEditText(Context context) {
            super(context.getApplicationContext());
        }
    
        public BaseEditText(Context context, AttributeSet attrs) {
            super(context.getApplicationContext(), attrs);
        }
    
        public BaseEditText(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context.getApplicationContext(), attrs, defStyleAttr);
        }
    
        @Override
        protected void onDetachedFromWindow() {
            try {
                if (mParent != null)
                    mParent.set(this, null);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            super.onDetachedFromWindow();
        }
    }
    

    相关文章

      网友评论

          本文标题:android内存泄漏常见情况以及解决方法

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