Reputation: 2553
I am using a ListView to show a list of books using Google API. I have maximum 40 books shown with book cover loaded using Volley ImageLoader. It was working fine.
Then today I decided to add an infinite scroller to my list view which loads 40 next books every time I scroll to the bottom. The issue is, I am getting out of memory error after around 80 books are loaded.
If I just comment out the ImageLoader part, I can scroll down to thousands without an issue.
So I am not sure what is causing the memory error issue, and how to solve that. Any help will be highly appreciated.
FYI, I am using Singletone to load imageLoader
ImageLoader method (inside ListView adapter)
private ImageLoader imageLoader = 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);
}
});
imageLoader.get(urlThumb, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean b) {
holder.thumb.setImageBitmap(response.getBitmap());
}
});
Infinite Scroller
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(!_loading && (totalItemCount - firstVisibleItem - visibleItemCount) < 5){
_loading = true;
// LOAD NEXT 40
}
}
Upvotes: 0
Views: 236
Reputation: 2224
Maybe your images are to big when you downloaded them. A single image with 800 x 600 pixels will be less than 100kb when it comes as JPG as it is compressed. However, when you decode it into an bitmap it will become approx. 2MB of data. When you load 80 books, you will get 160MB of data very soon. Mabye you should load less books (20) or reduce the size of your images from the server.
Usually, logcat prints an Exception which will show you what causes the OutOfMemoryException. When I got this exception it was always caused by too big or too many pictures at a time.
Upvotes: 1