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

  • SEARCH
  • Home
  • 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 7574751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:30:45+00:00 2026-05-30T16:30:45+00:00

I’m thoroughly stumped by this one. I have a static class which manages a

  • 0

I’m thoroughly stumped by this one. I have a static class which manages a cache of downloaded bitmap images. This class is accessed by multiple threads with potential for concurrent access. Here’s the implementation:

public class BitmapCache {
    private static final int MAX_NUMBER_BITMAPS_TO_CACHE = 30;
    private static Map<String, Bitmap> bitmapCache = new HashMap<String, Bitmap>();
    private static List<String> cachedBitmapUrlsOrder = new ArrayList<String>();

    private BitmapCache(){}

    public static synchronized void addBitmapToCache(String url, Bitmap bitmap) {
        if (bitmapCache.size() >= MAX_NUMBER_BITMAPS_TO_CACHE) {
            Log.i("MyApp", "Max cache size reached.  Removing oldest bitmap.  Size = " + bitmapCache.size());
            String oldestUrl = cachedBitmapUrlsOrder.remove(0);
            bitmapCache.remove(oldestUrl);
        }
        bitmapCache.put(url, bitmap);
        cachedBitmapUrlsOrder.add(url);
    }

    public static int size() {
        return bitmapCache.size();
    }

    public static Bitmap get(String url) {
        return bitmapCache.get(url);
    }

    public synchronized static void clearCache() {
        bitmapCache.clear();
    }
}

I’m trying to implement a rolling cache so that when the max cache size is reached (a common occurence) the oldest bitmap is removed from the cache. Running my application gives the following output:

02-29 23:00:26.590: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 30
02-29 23:00:26.600: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 30
02-29 23:00:26.720: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 30
02-29 23:00:26.790: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 30
02-29 23:00:26.820: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 31
02-29 23:00:26.850: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 31
02-29 23:00:27.050: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 32
02-29 23:00:27.070: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 32
02-29 23:00:27.100: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 33
02-29 23:00:27.130: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 34
02-29 23:00:27.170: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 35
02-29 23:00:27.210: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 35
02-29 23:00:27.330: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 35
02-29 23:00:27.360: I/MyApp(10789): Max cache size reached.  Removing oldest bitmap.  Size = 35

The code correctly starts logging ‘Max cache size reached’ when the cache size reaches 30 and stays there for a few executions. But then it strangely begins to increase up to 35. At this point it stays there for hundreds more output. I can’t get it to increase beyond 35.

What is wrong with my implemention? Given the addBitmapToCache method is synchronized I’m baffled at how the cache size can grow beyond the maximum set.

  • 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-05-30T16:30:46+00:00Added an answer on May 30, 2026 at 4:30 pm

    Several problems.

    Firstly, cachedBitmapUrlsOrder is a List. What happens to it if the same bitmap is requested more than once? You get a whole bunch of duplicate URLs in the list. So the first time you hit the limit, you remove a URL from the list, and from the map. But the same URL is still in the list, with no matching map entry. So subsequent attempts to remove the same URL from the list will not remove anything from the map, and the map will grow.

    You can avoid this by checking, right at the start of addBitmapToCache(), if the URL is already cached, with bitmapCache.containsKey(url). If so, you don’t need to modify the map; just make sure this entry is remembered as the most recent.

    Alternatively, change the list to be a collection of unique values; probably a map of URLs to timestamps, or a Set. (I’ll let you decide.)

    Also, the get() and size() methods should be synchronized too.

    And get() should update the timestamp of the retrieved entry, so that it becomes the most recent.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.