1

I have an application where I need to get the imageView height and width at run time. I have found a solution using the post method which gets called after the View has been drawn. I have a huge problem of memory leak in the app at the moment. I have been using Android Monitor, MAT and leak canary for the last few days. I have removed reference to everything that caused a leak but the actual problem cannot be traced. I suspect that the post method might actually be holding reference to the activity/fragment. I am not sure. Will the following snippet of code actually cause memory leak or am I looking in the wrong place.

 imageView.post(new Runnable() {
        @Override
        public void run() {
            Picasso.with(getActivity().getApplicationContext())
                    .load(R.drawable.download)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .resize(imageView.getWidth() / 3, imageView.getHeight() / 3)
                    .noFade()
                    .error(R.drawable.gradient_background_shop)
                    .into(imageView);
        }
    });

Also, I am using the Volley library for data fetching. But I have noticed Leak Canary hinting that the Network Dispatcher is leaking memory. I am using the singleton pattern in my Application class where volley is initialised. Does Volley actually cause issues?

public class YouStyleMe extends Application {

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static YouStyleMe mInstance;
public static Context mCtx;
public static final String TAG = YouStyleMe.class
        .getSimpleName();


public void onCreate() {
    super.onCreate();
    LeakCanary.install(this);
}

public YouStyleMe() {
}

private YouStyleMe(Context context) {
    mCtx = context;
    mRequestQueue = getRequestQueue();

    mImageLoader = new ImageLoader(mRequestQueue,
            new ImageLoader.ImageCache() {
                private final LruCache<String, Bitmap>
                        cache = new LruCache<String, Bitmap>(20);

                @Override
                public Bitmap getBitmap(String url) {
                    return cache.get(url);
                }

                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                    cache.put(url, bitmap);
                }
            });
}


public static synchronized YouStyleMe getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new YouStyleMe(context);
    }
    return mInstance;
}


public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        // getApplicationContext() is key, it keeps you from leaking the
        // Activity or BroadcastReceiver if someone passes one in.
        mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);

}

public ImageLoader getImageLoader() {
    return mImageLoader;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    // set the default tag if tag is empty
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public void cancelPendingRequests() {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(TAG);
    }
 }
}
  • 2
    `mInstance = new YouStyleMe(context);` ... `YouStyleMe extends Application` ... maybe it is not connected with memory leak but ... very, very wrong – Selvin Feb 03 '16 at 15:59
  • shouldn't `mInstance` be initialized inside a `static { ... }` block? – Shark Feb 03 '16 at 16:04
  • Refer to this answer please : http://stackoverflow.com/a/43723971/5860087 This might solve your purpose. Ciao. – Jatin Jha May 01 '17 at 19:38

1 Answers1

0

for problem 1) use a handler

initialize it like this:

private Handler mHandler = new Handler();

then, don't post an anonymous runnable to it, declare it like this:

updateCall = new WeakReference<Runnable>(new Runnable() {
        @Override
        public void run() {
            ...
        }
    });

and use either the handler, or the ImageView

imageView.post(updateCall.get());

those should help out a bit.

Another big problem is this line:

private static YouStyleMe mInstance;

because of a private static property, that object will have a really long life-span, and most of it's sub-objects (members) will be reachable forever. Which means GC won't get them, because it cleans only no-longer-reachable objects. Not even forcing GC will clean them up.

So, try to fix that, or make your mInstance a WeakReference as well.

Shark
  • 6,513
  • 3
  • 28
  • 50