Timer cause memory leak

by James Wang » Sat, 27 Feb 2010 11:58:36 GMT

Sponsored Links
 I have three activities:MyFirstActivity(click)->MyActivity(timer)-
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) {
        TimerTask task = new TimerTask() {
            public void run() {
                Intent intent = new Intent();

        timer.schedule(task, INTERVAL_TIME);

    public void 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?


Other Threads

1. Special tutorials

Hi all is there any special tutorial for Android new SDk apart from
DOC by google, if there send me its URL ..



2. Search Invoke sample

Question on the API demo - Search Invoke

I launched this sample and have selected "Automatic" in the spinner. I
also see a menu option "Automatic". When this menu option is selected,
it pops an alert that says "To invoke search try menu+S"

However, menu+S doesn't bring the search dialog.  Any issues?

3. Hide Emulator Keyboard

4. ListView and custom rows containing EditText

5. Problem with basic Map application

6. getScale replacement for BaseAdapter in 0.9 beta?

7. Bug in Bitmap.compress when used on JPEGs?