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. Window manager for Android


I was going through the window manager of android. I wonder if is it
possible to replace this window manger with out own window manger. Is
this supported? what depencies are there for this?

please answer this question if anybody knows
thanks in advance


2. antialiasing on an overlay?

Hi guys,

I've got an overlay working to draw regular canvas operations on top
of an opengl es surface view.  It works great, and was easier to
implement than I expected; thanks Android devs!  Here's my problem:

I first clear my overlay canvas with drawArgb(0, 0, 0, 0).  This gives
it a clear background so that when I paste it on top of my opengl
view, it doesn't overwrite everything.  I'm sure you guys are familiar
with this.  The thing is, when I use anti-aliasing to draw shapes, it
uses the background rgb colors (0, 0, 0) to anti-alias, and seems to
ignore the alpha component of the background I've cleared.  The result
is that I have blackish pixels drawn around the edges of whatever I am
drawing into the overlay when I paste it onto the opengl surface.

I hope I've stated my problem clearly.  Any suggestions?

3. Persisting Intents to Database

4. Using XPath libraries in Android

5. ISDN (Integrated Services Digital Network)

6. Gmail Notification

7. Vodafone HTC Magic skin now available for Android Emulator!