I have three activities:MyFirstActivity(click)->MyActivity(timer)-
>MyNextActivity.
And I noticed Timer cause MyActivity memory leak when digging through
hprof in Eclipse.
Below is my codes:
public class MyActivity extends Activity {
private Timer timer = new Timer();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TimerTask task = new TimerTask() {
public void run() {
Intent intent = new Intent();
intent.setClassName(getPackageName(),
MyNextActivity.class.getName());
startActivity(intent);
finish();
}
};
timer.schedule(task, INTERVAL_TIME);
}
public void onDestroy() {
super.onDestroy();
//memory leaking happens if there is not below.
//timer = null;
}
}
If I did set timer to null in onDestroy, MyActivity will always exist
in heap. I did the flow four times and found four timers and four
MyActivity instances in heap.
Who can tell me why?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en