I am using Volley ImageLoader to load images from server and display theme inside my RecyclerView, here is code of my ImageLoader:
mImageLoader=new ImageLoader(mRequestQueue,new ImageLoader.ImageCache() {
private LruCache<String, Bitmap> cache=new LruCache<>((int)(Runtime.getRuntime().maxMemory()/1024)/8);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
Im loading photos in my Adapter:
if (currentItem.myPlikPhotoUrl != null){
imageLoader.get(
currentItem.myPlikPhotoUrl,
new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
holder.myPlikPhoto.setImageBitmap(imageContainer.getBitmap());
}
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
Everything works Ok, but when i am scrolling too fast i get OOM exceptions, there is some large images, so it may be hard to work with, how should i work with this kind of issues, and what is the right way of handling image loading?
Logcat error:
09-29 17:53:48.024 4629-4657/com.plikster.plikster I/art﹕ Clamp target GC heap from 66MB to 64MB
09-29 17:53:48.024 4629-4657/com.plikster.plikster I/art﹕ HomogeneousSpaceCompact marksweep + semispace GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 2% free, 62MB/64MB, paused 8.700ms total 8.700ms
09-29 17:53:48.024 4629-4657/com.plikster.plikster W/art﹕ Throwing OutOfMemoryError "Failed to allocate a 4283669 byte allocation with 1545568 free bytes and 1509KB until OOM"
09-29 17:53:48.033 4629-4658/com.plikster.plikster I/art﹕ Starting a blocking GC Alloc
09-29 17:53:48.033 4629-4658/com.plikster.plikster I/art﹕ Starting a blocking GC Alloc
09-29 17:53:48.035 4629-4657/com.plikster.plikster E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-465
Process: com.plikster.plikster, PID: 4629
java.lang.OutOfMemoryError: Failed to allocate a 4283669 byte allocation with 1545568 free bytes and 1509KB until OOM
at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122)
at com.android.volley.toolbox.BasicNetwork.entityToBytes(BasicNetwork.java:257)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:130)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)
09-29 17:53:48.043 4629-4658/com.plikster.plikster I/art﹕ Alloc sticky concurrent mark sweep GC freed 57(9KB) AllocSpace objects, 0(0B) LOS objects, 2% free, 62MB/64MB, paused 1.877ms total 8.844ms
09-29 17:53:48.044 4629-4658/com.plikster.plikster I/art﹕ Starting a blocking GC Alloc
09-29 17:53:48.119 4629-4660/com.plikster.plikster W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-29 17:53:48.119 4629-4660/com.plikster.plikster W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x9febd780, error=EGL_SUCCESS
09-29 17:53:48.265 4629-4658/com.plikster.plikster I/art﹕ Alloc partial concurrent mark sweep GC freed 149(9KB) AllocSpace objects, 1(4MB) LOS objects, 6% free, 58MB/62MB, paused 50.522ms total 213.856ms
09-29 17:53:49.629 4629-4660/com.plikster.plikster E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xb4017a20
There was other errors befor i changed ImageView to NetworkImageView...