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. Making a background app like the clock?

Hi,

How do we go about making applications that can run directly on the
Home screen in their own discreet UI container like the analog clock?

Regards,
Steve
--~--~---------~--~----~------------~-------~--~----~

2. getChildrenCursor() JOINs

ExpandableList2.java uses a content provider. I'm using SQLite on it's
own. What type of Join should I implement in the getChildrenCursor()
method?


- Juan T.
--~--~---------~--~----~------------~-------~--~----~

3. How do you compile the bundled apps that comes with the Android source code

4. Some functionality of Android is locked

5. Padding Not Working in XML but does in Java

6. BouncyCastle PKCS12 - dead?

7. Loading application to actual device through Eclipse?