Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android memory leaks points are not clear!

Tags:

android

friends,

i have read complete article related to avoiding memory leaks in android. http://developer.android.com/resources/articles/avoiding-memory-leaks.html

right now

1) i am using private nested class not static

if i make that nested class static will it be usefull?

2) article says If you're about to use Inner Classes or Anonymous Classes think carefully. Don't use Anonymous Classes until you're very sure and can prove that they are not causing a Memory Leak.

can any one give me example of that? which one is good approach and which one bad for memory leaks.

any help would be appreciated.

like image 658
UMAR-MOBITSOLUTIONS Avatar asked Nov 14 '10 10:11

UMAR-MOBITSOLUTIONS


1 Answers

1) I would avoid using static classes in general. Especially if you need to pass in the Context pointer, as this will cause a leak. Unless you static classes have on constants, they are analogous to global variables and kinda circumvent the Android architecture which is meant to decouple activities.

Especially you don't want to declare Drawable instances or Android framework objects as static. This messes up their lifetime.

2) I haven't seen any problems with anonymous classes in particular. You may in some cases be able to leak a Context variable, but this is hard to do on a single thread. When passing a context around, you can limit leaks by using getApplicationContext(), which returns the global context which will not leak.

Hope this helps!

like image 169
swinefeaster Avatar answered Nov 15 '22 00:11

swinefeaster