Android中的内存泄露问题

前言:

内存泄漏(Memory Leak)是指程序中己动态分配的堆内存由于某种原因程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果。

Android中发生内存泄露的常见情形如下:

1.handler引起的内存泄露

private TextView textView;
private Button button;
private Handler mHandler  = new Handler(){
    @Override
    public void handleMessage(@NonNull Message msg) {
        super.handleMessage(msg);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
}
​
​
private void initView() {
    textView = findViewById(R.id.tv_text);
    button = findViewById(R.id.btn_start);
  new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(50000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}).start();
​
Message message = Message.obtain();
mHandler.sendMessageDelayed(message, 5 * 60 * 1000);

}
​
​
@Override
protected void onDestroy() {
    super.onDestroy();
    RefWatcher refWatcher = App.getRefWatcher(this);
    refWatcher.watch(this);
}
​
public void clickView(View view) {
    switch (view.getId()) {
        case R.id.btn_start:
            Toast.makeText(MainActivity.this, "View是" + view, Toast.LENGTH_SHORT).show();
            break;
        case R.id.tv_text:
            Log.e("View", view.toString());
            Toast.makeText(MainActivity.this, "View是" + view, Toast.LENGTH_SHORT).show();
            break;
    }
​
}

2.旋转屏幕会发生内存泄露,内存泄露截图:

img

3.解决方法:

private static Runnable mRunnable = new Runnable() {
    @Override
    public void run() {
        while (!isStop){
            //执行操作
            try {
                Thread.sleep(50000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
};

img

3.1 Runnable定义为静态内部类,并且定义一个标记是否一加停止,在onDestroy把标记设为true

3.2 Handler定义为静态内部类,并改为软引用方式,在Ondestroy()方法移除所有的message和callback

img

4.解决后的完整代码如下:不管屏幕怎么旋转都没有发生泄露

/**
 * @作者: njb
 * @时间: 2019/11/20 13:12
 * @描述: handler引起的内存泄露
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity.class";
    private TextView textView;
    private Button button;
    private static MyHandler mHandler;
    private static boolean  isStop = false;
    private static Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            while (!isStop){
                //执行操作
                try {
                    Thread.sleep(50000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
​
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
​
​
    private void initView() {
        textView = findViewById(R.id.tv_text);
        button = findViewById(R.id.btn_start);
        mHandler = new MyHandler(MainActivity.this);
        new Thread(mRunnable).start();
    }
​
​
    private static class MyHandler extends Handler {
        private WeakReference<Activity> weakReference;
​
        private MyHandler(Activity activity) {
            weakReference = new WeakReference<>(activity);
        }
​
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            final Activity activity = weakReference.get();
            if (msg.what == 1) {
                if (activity != null) {
                    Message message = Message.obtain();
                    mHandler.sendMessageDelayed(message, 5 * 60 * 1000);
                }
            }
        }
    }
​
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        isStop = true;
        RefWatcher refWatcher = App.getRefWatcher(this);
        refWatcher.watch(this);
    }
​
    public void clickView(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                Toast.makeText(MainActivity.this, "View是" + view, Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, LeakThreadActivity.class));
                break;
            case R.id.tv_text:
                Log.e("View", view.toString());
                Toast.makeText(MainActivity.this, "View是" + view, Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, HorizontalScrollViewActivity.class));
                break;
        }
    }
}

5.线程引起的内存泄露

/**
 * @作者: njb
 * @时间: 2019/11/20 13:12
 * @描述: 线程引起的内存泄露
 */
public class LeakThreadActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leak_therad);
        initThread();
    }
​
    private void initThread() {
        LeakThread leakThread = new LeakThread();
        leakThread.start();
    }
​
     class LeakThread extends Thread {
        @Override
        public void run() {
            try {
                Thread.sleep(6 * 60 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
​
    @Override
    protected void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = App.getRefWatcher(this);
        refWatcher.watch(this);
    }
}

6.旋转屏幕后,内存泄露截图如下:

img

7.解决方法:线程改为静态内部类

img

/**
 * @作者: njb
 * @时间: 2019/11/20 13:12
 * @描述: 线程引起的内存泄露
 */
public class LeakThreadActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leak_therad);
        initThread();
    }
​
    private void initThread() {
        LeakThread leakThread = new LeakThread();
        leakThread.start();
    }
​
     static class LeakThread extends Thread {
        @Override
        public void run() {
            try {
                Thread.sleep(6 * 60 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
​
    @Override
    protected void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = App.getRefWatcher(this);
        refWatcher.watch(this);
    }
}

8.以上就是几种内存泄露场景和解决方法,后面还会给出资源未回收关闭等各种情况

小伙伴们记得在Activity退出或者关闭及时回收各种资源,比如webview、rxbus、广播、bitmap等,写得不好还望大家见谅,小伙伴们有问题提出,我会及时更正.

9.项目源码地址:

Memoryleak: Android中的内存泄露实例

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值