Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8896799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:10:11+00:00 2026-06-15T00:10:11+00:00

I have a listview that probably has infinite items loaded on scrolling infinitely. Each

  • 0

I have a listview that probably has infinite items loaded on scrolling infinitely.

Each item in list view has one or two images which I’m lazy loading.

Everything works great but when I scroll for really long it crashes with this in log cat

 08-07 15:26:25.231: E/AndroidRuntime(30979): FATAL EXCEPTION: Thread-60
08-07 15:26:25.231: E/AndroidRuntime(30979): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
08-07 15:26:25.231: E/AndroidRuntime(30979):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-07 15:26:25.231: E/AndroidRuntime(30979):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
08-07 15:26:25.231: E/AndroidRuntime(30979):    at com.test.android.helper.LazyImageLoader.decodeFile(LazyImageLoader.java:171)
08-07 15:26:25.231: E/AndroidRuntime(30979):    at com.test.android.helper.LazyImageLoader.getBitmap(LazyImageLoader.java:112)
08-07 15:26:25.231: E/AndroidRuntime(30979):    at com.test.android.helper.LazyImageLoader.access$2(LazyImageLoader.java:106)
08-07 15:26:25.231: E/AndroidRuntime(30979):    at com.test.android.helper.LazyImageLoader$ImageLoader.run(LazyImageLoader.java:197)

In my lazy image loader I am storing bitmaps in a WeakHashMap. So garbage collector should collect the bitmaps right?

My lazy imageloader works something like this.

I call displayImage() from my adapter with url and a reference to imageview

public void displayImage(String url, ImageView imageView, int defaultImageResourceId){

        latestImageMetaData.put(imageView, url);

        if(weakhashmapcache.containsKey(url)){
            imageView.setImageBitmap(weakhashmapcache.get(url));
        }
        else{
            enqueueImage(url, imageView, defaultImageResourceId);
            imageView.setImageResource(defaultImageResourceId);
        }
    }

So if I find the image in cache, I set it directly, otherwise I queue it with function enqueueImage().

private void enqueueImage(String url, ImageView imageView, int defaultImageResourceId){
Image image = new Image(url, imageView, defaultImageResourceId);
downloadqueue.add(image);
// downloadQueue is a blocking queue which waits for images to be added
//If the queue is about to get full then delete the elements that are ahead in the queue as they are anyway not visible
Iterator iterator = downloadQueue.iterator();
while(iterator.hasNext() && downloadQueue.remainingCapacity() < 80){
downloadQueue.remove(iterator.next());
}
}

And my image loader thread is this - 

class ImageLoader extends Thread {

    public void run() {
        Image firstImageInQueue;

        try {
            while((firstImageInQueue = downloadQueue.take()) != SHUTDOWN_TOKEN)
            {
                Bitmap imageBitmap = getBitmap(firstImageInQueue.url);

                if(imageBitmap != null){
                    weakhashmap.put(firstImageInQueue.url, imageBitmap);
                    BitmapDisplayer displayer = new BitmapDisplayer(imageBitmap, firstImageInQueue.imageView, firstImageInQueue.url, firstImageInQueue.defaultImageResourceId);
                    Activity activity = (Activity)firstImageInQueue.imageView.getContext();
                    activity.runOnUiThread(displayer);
                }
            }
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            imageLoaderTerminated = true;
        }
    } 
}

getBitmap() just fetches image from url scales and decodes it into a Bitmap object. BitmapDisplayer is just a Runnable which does the setting of image to imageview on UI thread.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:10:13+00:00Added an answer on June 15, 2026 at 12:10 am

    It has been a nightmare and after days & nights of research here are few points that may be useful to others.

    Don’t store all the Bitmaps in cache. Keep it swapping between Disk cache and Memory Cache. Number of bitmaps you store can depend on the heap limit that you get by calling

    int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
    .getMemoryClass();

    I used a LruCache instead of WeakHashMap cache. LruCache is available in the support package. It is very easy to replace your existing WeakHashMap implementation with LruCache. Android also has a beautiful documentation on Caching Bitmaps.

    Jake Wharton’s DiskLruCache is a great way to manage Disk Cache.

    Don’t download huge bitmaps if you do not need it. Try to get a size that’s just good enough to fit your need.

    Using BitmapFactory.Options you can make some trade offs with image quality to hold more images in memory cache.

    If you think there’s anything more we could do, please add.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problems in implementing a list view with 3 items, one text,
I have a ListView that creates a table. Each row in the table has
I have a ListView that contains button on each item. I want that the
I have ListView that has the following EditItemTemplate: <EditItemTemplate> <tr style=> <td> <asp:LinkButton ID=UpdateButton
I have a ListView that shows around 300 items. When something is changed and
I have a ListView that has a custom ArrayAdapter with a custom XML row.
I have a listview that is binded to a ThreadSafeObservableCollection. The background of each
I have a page that can be viewed by list or grid view through
I have ListView that shows data: <ListView android:id=@+android:id/list android:layout_width=fill_parent android:layout_height=wrap_content> </ListView> The headlines is
Probably a simple question but one that eludes me nonetheless. I have a class:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.