[英]Memory leak with Timer
I am using a timer that is canceled and restarted on a listener event. 我正在使用一个在侦听器事件中取消并重新启动的计时器。 It all works fine except that the the timer thread leaks the whole outer class. 除了计时器线程泄漏整个外部类之外,一切正常。
My timer implementation is as follows: 我的计时器实现如下:
if(timer != null) {
timer.cancel();
timer = null;
timer = new Timer();
}
timer.schedule(new TimerTask() { // Thread leaks!!!!
@Override
public void run() {
mCallback.onHeaderMoving(newToolbarTranslationY );
}
} , 150);
I used MAT Analyser to track down the problem and ended up there. 我使用MAT Analyzer来追踪问题并最终到达那里。 I also commented out the line with the callback but the thread still leaks so it is defenetly the timer itself. 我还注释了回调线,但是线程仍然泄漏,所以它本身就是计时器本身。 However I don't really understand what is the problem with that code. 但是,我真的不明白该代码的问题是什么。
As far as I understand from my research the problem is that the anonymous inner class (new Timertask()) holds a reference to the outer class and therefore can leak the whole context. 据我所知,我的研究问题是匿名内部类(new Timertask())拥有对外部类的引用,因此可以泄漏整个上下文。 But I still don't understand why the timer and also the reference to the context is not garbage collected after the thread runs out (after 150 ms +). 但是我仍然不明白为什么在线程用完之后(150毫秒+之后),定时器以及对上下文的引用不会被垃圾收集。
Is the context in this case somehow still not released even after the thread finished? 在这种情况下,即使在线程完成之后,上下文仍然不会被释放吗?
And finally how do I solve this leak? 最后我该如何解决这个漏洞? I set the timer to null but that didn't solved my problem. 我将计时器设置为null但这并没有解决我的问题。
Edit 编辑
private OnHeaderMovingCallBack mCallback;
private Timer timer = new Timer();
//... some other parameters
public ScrollingToolbarManager(View toolbar , View pagerStrip , AbsListView listView , OnHeaderMovingCallBack headerMovingCallBack){
this.toolbar = toolbar;
this.pagerStrip = pagerStrip;
this.listView = listView;
mCallback = headerMovingCallBack;
changeStartValues();
}
public static interface OnHeaderMovingCallBack{
public void onHeaderMoving(int translationY);
}
public void moveHeader(){
//... some calculations
//timer implementation from above
}
moveHeader() is called on a scroll event of a listview 在listview的滚动事件上调用moveHeader()
If you think that the problem is that the anonymous inner class holds a reference to the outer class, then simply use a static named inner class - this will hold no reference. 如果你认为问题是匿名内部类持有对外部类的引用,那么只需使用静态命名的内部类 - 这将不包含引用。 Put something like this inside your class: 在你的课堂上放置这样的东西:
static class MyTimerTask extends TimerTask {
private OnHeaderMovingCallBack mCallback;
int newToolbarTranslationY;
public MyTimerTask(OnHeaderMovingCallBack mCallback, int newToolbarTranslationY) {
this.mCallback = mCallback;
this.newToolbarTranslationY = newToolbarTranslationY;
}
@Override
public void run() {
mCallback.onHeaderMoving(newToolbarTranslationY);
}
}
I have the same problem with you. 我和你有同样的问题。 I found that when I define Timer
as global var and don't set it to null
when the activity finished, it always leads memory leak. 我发现当我将Timer
定义为全局var并且在活动结束时不将其设置为null
时,它总是导致内存泄漏。 And when I define Timer
as local var or set it to null
, the problem gone.But I don't understand why. 当我将Timer
定义为局部var或将其设置为null
,问题就消失了。但我不明白为什么。 If you had solved it, please tell me your solution, thanks! 如果你已经解决了,请告诉我你的解决方案,谢谢!
public class TestActivity extends AppCompatActivity {
private Timer mTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 0);
}
@Override
protected void onDestroy() {
super.onDestroy();
mTimer = null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.