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);
}
}
}