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. mail.jar activation.jar problem in android

Could you give some more details on what you are trying to do...

2008/10/3 [EMAIL PROTECTED] <[EMAIL PROTECTED]>


--~--~---------~--~----~------------~-------~--~----~

2. Capabilities of Location API

We want to develop an application that uses the GSM network to get a
position.


There are several methods for getting a position based on cell tower
info but to achieve a acceptable level of accuracy the info (ID,
signal strength, ...) of *all* visible cell towers is needed, not just
from the currently connected one.

Does the Android SDK provide access to this information?


The SDK seems to provide a way to get a position via the
android.location.LocationManager. The JavaDoc of the NETWORK_PROVIDER
constant states "Results are retrieved by means of a network lookup".

What technology is used by this method?


Thanks.


PS
I posted a similar question earlier today but I never saw it appear.
If it did get through, my apologies for this double post.

--~--~---------~--~----~------------~-------~--~----~

3. How shall I need design my application when the device be rotated?

4. Customizing builtin applications

5. Android and webservice Apache Axis

6. Call an application from another application in android

7. The method onOptionsItemSelected(MenuItem) of type AndroidViews must override or implement a supertype method