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. Widget button dies

I have a wdiget containing an imageView. On this imageView i put a
pendingIntent so i can click on it.
It works, but sometimes (can be once a day) the pendingIntent is not
sent, and the widget does nothing.
I have to delete the widget and put it again to get it work.

How can i have a permanent pendingIntent on my imageView ?

The pendingIntent is added in the onUpdate method of my main class
which extends AppWidgetProvider


2. Android and Java Reflection to call a higher API level function/abstract class

Hello there.

My point is to be able to call a 2.1 API if it exists, while the
application should be able to run in 1.5

Specifically, I am making a GPS application which would like to call
android.location.LocationManager.addNmeaListener if available.

My problem is that this call takes as parameter an abstract class
(GpsStatus.NmeaListener) which I cannot pass directly to
Class.GetMethod (or my application won't work in 1.5).

What I am currently trying is a) to create a wrapper class for

public class APILevel5
        // class initialization fails when this throws an exceptio
                try {
                        //      We don't have to check for existance because it 
is an abstract
                catch (Exception ex)
                        throw new RuntimeException(ex);


    // calling here forces class initialization
    public static void checkAvailable() {}

    // Forced Constructor
    public static GpsStatus.NmeaListener Creator()
        GpsStatus.NmeaListener mGpsNmeaListener = new
                        public void onNmeaReceived(long timestamp, String nmea)
        return mGpsNmeaListener;

When checkAvailable() is called from 1.5, it throws. When called from
2.1, it doesn't. All good so far.

Now how to call the method ? I am using this reflection code:

        public static Object CallMethod(Class x,String mn,Class... typs)
                        m = x.getMethod(mn, typs);
                        return m.invoke(null,(Object)typs);
                catch(NoSuchMethodException e)
                catch(InvocationTargetException  e)
                catch (IllegalAccessException ie)
                return null;

When I call CallMethod(LocationManager.class,"addNmeaListener" , new
Class[] { GpsStatus.NmeaListener.class }); , it works in 2.1 but the
application doesn't run at all in 1.5

When I call CallMethod(LocationManager.class,"addNmeaListener" , new
Class[] { APILevel5.class }); , the application runs in 1.5 but
doesn't find the method because APILevel5.class is passed and not

How can I resolve all this Java stuff ?

Best Regards,


3. Invisible Activity for Android > 1.6

4. Are Android apps developed with the NDK slower than their counterparts on other OS's?

5. WebView / Android Browser - touch events not firing

6. Calendar Event Type

7. Installing Program / Pushing Data to Multiple Devices Connected Via USB