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. librt (Async IO) for Android ?

Is it possible to build librt (AIO support) for Android...? I am a bit
new to this and I did try, writing up a an Android.mk and simply
attempting to "mm". But, I get a bunch of errors as follows:


external/librt/aio.h:49: error: expected specifier-qualifier-list
before '__ssize_t'
external/librt/aio.h: In function 'aio_read':
external/librt/aio.h:145: error: expected declaration specifiers
before '__THROW'
external/librt/aio.h:147: error: expected '=', ',', ';', 'asm' or
'__attribute__' before '__THROW'
external/librt/aio.h:151: error: '__restrict_arr' undeclared (first
use in this function)
external/librt/aio.h:151: error: (Each undeclared identifier is
reported only once
external/librt/aio.h:151: error: for each function it appears in.)
external/librt/aio.h:153: error: expected '=', ',', ';', 'asm' or
'__attribute__' before '__THROW'
external/librt/aio.h:156: error: expected '=', ',', ';', 'asm' or
'__attribute__' before '__THROW'
external/librt/aio.h:158: error: expected '=', ',', ';', 'asm' or
'__attribute__' before 'aio_return'
external/librt/aio.h:162: error: expected '=', ',', ';', 'asm' or
'__attribute__' before '__THROW'
external/librt/aio.h:171: error: expected '=', ',', ';', 'asm' or
'__attribute__' before '__nonnull'
external/librt/aio.h:176: error: expected '=', ',', ';', 'asm' or
'__attribute__' before '__THROW'
In file included from external/librt/aio_cancel.c:31:
bionic/libc/include/errno.h:45: error: storage class specified for
parameter '__set_errno'
bionic/libc/include/errno.h:48: error: storage class specified for
parameter '__errno'
external/librt/aio_cancel.c:35: error: expected '=', ',', ';', 'asm'
or '__attribute__' before '{' token
external/librt/aio_cancel.c:40: error: expected declaration specifiers
before 'weak_alias'
make: Leaving directory `/home/dixit/projects/eclair'
external/librt/aio_cancel.c:44:22: error: stub-tag.h: No such file or
directory


Any help/pointers would be useful. I know I am messing this up, but
being a newbie, Iam not able to relate what the error amounts to.


Thanks,
Vijay

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

2. Changing entry activity causes app only to open from downloads page

On Mon, Jun 14, 2010 at 6:40 PM, Christopher Perry <




I recently ran into this as well.

In my experience, the update installation process appears to sometimes screw
things up and causes completely random, undesired behavior like force
closing, terrible network connectivity, or this inability to launch the app,
as you just noticed.

Ask your users to uninstall and reinstall the app (possibly with a reboot in
between). I've found this fixes these random issues about 99.9% of the time.

-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices
http://sites.google.com/site/rezmobileapps/treking

-- 

3. apk including linux binary

4. How to debug on device with Eclipse/ADB ?

5. How to (properly) get device orientation data?

6. notifyDataSetChanged() not updating listview after orientation change

7. Getting FileNotFound Exception when using FileReader