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. problems removing childs from view

hello,

I have some problems removing views.

Inside my xml I have a LinearLayout with a ScrollView with a
TableLayout.

Into this I am adding (using Java) childs, which are LinearLayouts (no
TableRows).

This works fine.

Now I want to remove some childs, which are LinearLayouts.

linearlayout.setVisibility(View.GONE) works finde but doesnt remove
the Object itself.

tablelayout.removeView(linearlayout) works only under strange
circumstances: removing the last element of my list works fine.
removing some element in the middle throws this exception:

                                Uncaught handler: thread main exiting due to 
uncaught exception
                                java.lang.NullPointerException
                                at 
android.view.ViewGroup.dispatchDraw(ViewGroup.java:1207)
                                at 
android.view.ViewGroup.drawChild(ViewGroup.java:1434)
                                at 
android.view.ViewGroup.dispatchDraw(ViewGroup.java:1208)
                                at android.view.View.draw(View.java:5571)
                                at 
android.widget.FrameLayout.draw(FrameLayout.java:324)
                                at 
android.view.ViewGroup.drawChild(ViewGroup.java:1436)
                                at 
android.view.ViewGroup.dispatchDraw(ViewGroup.java:1208)
                                at 
android.view.ViewGroup.drawChild(ViewGroup.java:1434)
                                at 
android.view.ViewGroup.dispatchDraw(ViewGroup.java:1208)
                                at 
android.view.ViewGroup.drawChild(ViewGroup.java:1434)
                                at 
android.view.ViewGroup.dispatchDraw(ViewGroup.java:1208)
                                at 
android.view.ViewGroup.drawChild(ViewGroup.java:1434)
                                at 
android.view.ViewGroup.dispatchDraw(ViewGroup.java:1208)
                                at android.view.View.draw(View.java:5465)
                                at 
android.widget.FrameLayout.draw(FrameLayout.java:324)

linearlayout.removeAllViews() I tried in order to clear all childs of
my linearlayout (which are some buttons and textviews in fact), but
this doesnt change anything.

Any idea what I am doing wrong?

Greetings,
DaRolla
--~--~---------~--~----~------------~-------~--~----~

2. removing Phone.apk from the device

Hi All,
          To remove PhoneApp form the device, i followed the below
setps:

1) Execute 'adb remount' to mount /system folder in rw mode.
2) telnet 192.168.16.2 to logint to phone.
3) Execute 'rm /system/app/Phone.apk' to remove apk.
4) Execute 'rm /data/dalvik-cache/sys...@app@phone....@classes.dex'
to remove the running instance of the Phone app
5) Execute ' cd /data/data' and 'rm -R rm -R com.android.phone' to
remove the personal data of Phone App.

Even after doing all these setps, PhoneApp is still running on the
device.

Is it due to the android:persistent="true"attribute in
AndroidManifest.xml file.

Could you please help me in removing Phone App from the device.

Cheers
RajaniKanth
--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

3. GPS emulation

4. List view scroll issue

5. has leaked service connection in activity that was originally bound here

6. text alignment in edit text

7. How to programable send multi-attachment in an email