Static -[Memory Leaks]

This post is part continuation of Memory Leaks in Android

Static Views,Fragments,Activities or  listeners are one of biggest reasons for memory leaks in Android.

Reason is once your activity is destroyed, static references will not be garbage collected, and if it can’t be garbage collected thats leak whole activity

Problem:

You can see in below gif, how this perfectly static TextView is causing memory leak, Which is getting detected via awesome LeakCanary lib.

leakystatic

Solution:

Never make your views,Fragments,activities or listeners static. Basically any object which is having reference to Activity context, should never be static.

 

Leave a comment