leakcanary https://github.com/square/leakcanary
A,B两个Activity,A跳转到B,B用CountDownTimer计时,并且不做取消处理,然后立刻返回到A
先初始化LeakCanary
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
return;
}
LeakCanary.install(this);
}
}
Activity B 主要代码
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
countDownBtn = (Button) findViewById(R.id.countDownBtn);
timer = new CountDownTimer(60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
countDownBtn.setText(String.format("%d", millisUntilFinished));
}
@Override
public void onFinish() {
}
};
}
返回A后,leak就会提示内存泄漏
然后会收到推送,点击推送可以看内存泄漏的详情
可以看到是SecondActivity的CountDownTimer引起了内存泄漏