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 8988047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:56:01+00:00 2026-06-15T21:56:01+00:00

I am building an android app where each entity has a bitmap that represents

  • 0

I am building an android app where each entity has a bitmap that represents its sprite. However, each entity can be be duplicated (there might be 3 copies of entity asdf for example).

One approach is to load all the sprites upfront, and then put the correct sprite in the constructors of the entities.

However, I want to decode the bitmaps lazily, so that the constructors of the entities will decode the bitmaps. The only problem with this is that duplicated entities will load the same bitmap twice, using 2x the memory (Or n times if the entity is created n times).

To fix this, I built a SingularBitmapFactory that will store a decoded Bitmap into a hash, and if the same bitmap is asked for again, will simply return the previously hashed one instead of building a new one. Problem with this, though, is that the factory holds a copy of all bitmaps, and so won’t ever get garbage collected.

What’s the best way to switch the hashmap to one with weakly referenced values? In otherwords, I want a structure where the values won’t be GC’d if any other object holds a reference to it, but as long as no other objects refers it, then it can be GC’d.

  • 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-15T21:56:03+00:00Added an answer on June 15, 2026 at 9:56 pm

    Pretty much what you said — make the Bitmap (object side of the map) a WeakReference instead of a Bitmap. Then you have to add an extra check to see if the reference is still valid before passing it back to your entities. Here is a quick sketch of the general idea.

    public class SingularBitmapFactory { 
        private HashMap <String, WeakReference<Bitmap>> cache = new HashMap<String, WeakReference<Bitmap>>();
    
        public Bitmap getBitmap(String key) {
            Bitmap image = null;
            WeakReference<Bitmap> ref = cache.get(key);
            if(ref != null) {
                image = ref.get();
            }
            if(image == null) {
                // Load image here ... 
                cache.put(key, new WeakReference<Bitmap>(image));
            }
            return image;   
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an Android app with OpenGL.I created 2 squares, each with their own
I'm building an Android app and a Blackberry app (same app different platforms). There
I'm building an Android app that's reads comments from an MS Power Point file.
It is my first time building an android app. Was wondering firstly, if there
I'm considering building an Android app for my HTC phone that contains tour guides
I'm building an Android app that will stream several radio stations from a Latin
I am building an Android App that will communicate over TCP with a server.
I'm building a WebWorks version of an Android app that's localised into 39 languages.
I'm building an Android app that takes advantage of OpenGL. As it stands, the
I'm building an android app and I'm currently having trouble retrieving a bitmap from

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.