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

一、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();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容