Android memory leak points are not clear!

friends,

I read the full article related to avoiding memory leaks in android. http://developer.android.com/resources/articles/avoiding-memory-leaks.html

right now

1) I use a private nested class, not static

if i make this nested static class useful?

2) says the article If you intend to use inner classes or anonymous classes, think carefully. Do not use Anonymous classes until you are sure, and you can prove that they do not cause memory leaks.

Can someone give me an example of this? which is a good approach and which is bad for memory leaks.

Any help would be appreciated.

+7
android
source share
1 answer

1) I would not use static classes at all. Especially if you need to pass a context pointer, as this will cause a leak. If you do not have static classes on constants, they are similar to global variables and can bypass the Android architecture, which is designed for decoupling actions.

In particular, you do not want to declare instances of Drawable or Android framework as static. It scares their life.

2) I did not see problems with anonymous classes in particular. In some cases, you can skip the Context variable, but this is difficult to do in a single thread. When traversing a context, you can limit leaks using getApplicationContext (), which returns a global context that will not leak.

Hope this helps!

+1
source share

All Articles