Reputation: 123
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...
Upvotes: 0
Views: 503
Reputation: 53734
Since you are already using volley, you can painlessly switch to NetworkImageView from ImageView and probably avoid these issues.
You can use ImageLoader and NetworkImageView in concert to efficiently manage the display of multiple images, such as in a ListView. In your layout XML file, you use NetworkImageView in much the same way you would use ImageView
The same page provides a sample code for an LRU cache which hopefully you will find useful.
UPDATE: If you continue to have issues with NetworkImageView you might need to switch to a Disk based cache implementation. A very popular disk cache is DiskLruCache by Jake Warton of ActionbarSherlock fame. Luckily I discovered a blog post which explains how the two have been integrated.
Upvotes: 1
Reputation: 780
Reduce the size of your cache.
...a cache that is too large can once again cause java.lang.OutOfMemory exceptions...
https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
Upvotes: 0