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) {
        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?

--



Other Threads

1. Question about resolution and minimum flash & RAM

This would not represent an android compatible device (the screen is smaller
than we support, and it sounds like you may remove things that are required
in the sdk), so I'm not sure how much help you can expect to get.  You do
have the full source code to play with, and changing the basic size of the
screen should be fairly simple as it is just based on the driver reporting
it...  but of course lots of the standard UI would break on such a small
screen, so you'll just need to dig in and work on things to make them do
what you want.




> website: 

2. My app runs fine on ADP1, but crashes the Motorola Droid?

My Droid has, sadly, spontaneously rebooted on me 4 times so far today, all
while in the Gmail app. (Granted, this is the worst it has ever been.) :(

I'm running Locale and Google Voice as services, so I suppose one of them
might be the culprit... but I sort of doubt that.

-- 
Sent from my Droid




You can find a number of reports from people who see their Droids
crash in regular intervals (https://supportforums.motorola.com/thread/
16278); users like to attribute such problems to whatever app they
recently used/installed, but it really sounds like a problem with the
system in general.

Unless you receive several such problems reports, I wouldn't worry.

Michael

-- 

3. How to tell if we were force killed for memory, in onCreate()?

4. sim cards and g1

5. How to tell if we were force killed for memory, in onCreate()?

6. back button pressed while in an application and go back to the home screen

7. Loading Object Hierarchy