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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:44:48+00:00 2026-05-21T07:44:48+00:00

Sorry it seems like a repeated question, BUT I think I don’t qualify to

  • 0

Sorry it seems like a repeated question, BUT I think I don’t qualify to any of the recommendations already posted.

I’ve a Gallery of maximum 20 images on my application. After playing a while flinging back and forth I’m getting OutOfMemoryError.

The strange thing is that I don’t hold any static references, and I’ve searched for possible memory leaks I can assure that I’ve not found one so far.

Anyway, 20 images (PNG of 100KB on average) doesn’t be like that much. And I’ve implemented a view cache, SoftReference holders for the bitmaps, etc.

Is it 20 PNG images of 100KB on average enough to kill my app?? seriously? how can I get rid of this? I’ve followed this great post also

http://blog.jteam.nl/2009/09/17/exploring-the-world-of-android-part-2/

Any more ideas?

This is the ImageCache:

public class AsyncImageLoader {

    private final String TAG = getClass().getSimpleName();
    private Context mContext;
    private HashMap<String, SoftReference<Bitmap>> mImageCache;

    public AsyncImageLoader(Context context) {
        mContext = context;
            mImageCache = new HashMap<String, SoftReference<Bitmap>>();
    }

    public Bitmap loadImage(final String identifier, final String imagePath, final ImageCallback imageCallback) {

        if (mImageCache.containsKey(imagePath)) {
            SoftReference<Bitmap> softReference = mImageCache.get(imagePath);
            Bitmap bitmap = softReference.get();
            if (bitmap != null) {
                Log.i(TAG, "Retrieving image from cache: " + imagePath);
                return bitmap;
            }
        }

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message message) {
                imageCallback.imageLoaded((Bitmap) message.obj, imagePath, identifier);
            }
        };

        new Thread() {

            @Override
            public void run() {
                Bitmap bitmap = loadImageFromPath(imagePath);
                mImageCache.put(imagePath, new SoftReference<Bitmap>(bitmap));
                Message message = handler.obtainMessage(0, bitmap);
                handler.sendMessage(message);
            }

        }.start();

        return null;
    }

    public Bitmap loadImageFromPath(String path) {

        if(!GeneralUtilities.isEmpty(path)) {
            Log.i(TAG, "Loading image: " + path);
            InputStream imageInputStream = null;

            try {               
                final AssetManager assetManager = mContext.getResources().getAssets(); 
                imageInputStream = assetManager.open(path);

                Bitmap bitmap = GeneralUtilities.decodeFile(imageInputStream);

                imageInputStream.close();

                return bitmap;
            } catch (final IOException e) {
                Log.e(TAG, e.getMessage());
            }        
        }

        return null;
    }

    public interface ImageCallback {
        public void imageLoaded(Bitmap imageBitmap, String imagePath, String identifier);
    }
}

and the method GeneralUtilities.decodeFile is:

public static Bitmap decodeFile(InputStream is){
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, o);

        //The new size we want to scale to
        final int REQUIRED_SIZE=140;

        //Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;

        while(true) {
            if(width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;

            scale *= 2;
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(is, null, o2);  
      }

And in the getView of the ArrayAdapter I’ve something like this:

final ImageView itemImage = cache.getHistoryImage();        
        //final ImageView itemFrame = cache.getFrame();

        String filename = item.getFilename().trim();

        itemImage.setTag("front_" + filename);

        Bitmap cachedImage = mAsyncImageLoader.loadImage("front_" + filename, filename, new ImageCallback() {

            public void imageLoaded(Bitmap imageBitmap, String imagePath, String identifier) {

                ImageView imageViewByTag = (ImageView) mGallery.findViewWithTag(identifier);
                if (imageViewByTag != null) {
                    imageViewByTag.setImageBitmap(imageBitmap);
                }
            }
        });

        itemImage.setImageBitmap(cachedImage);
  • 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-21T07:44:49+00:00Added an answer on May 21, 2026 at 7:44 am

    There seems to be a bug in the Android framework, although Google seems to deny it.

    Did you read through issue 8488?

    http://code.google.com/p/android/issues/detail?id=8488

    I am not sure if this applies to your code – but you might try the recommendations before setting/updating the image on the ImageView.

    Basically, it boils down to calling Bitmap.recycle(), nulling references (probably irrellevant in your case) and explicitly calling calling System.gc().

    The garbage collector seems to run asynchronously and a new might fail even though memory could be freed.

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

Sidebar

Related Questions

Sorry if this seems like a stupid question but can't find any info on
Sorry if this question seems a bit complex but I think its all related
Sorry if this seems like a stupid question but im actually having a hard
Sorry if this seems like a dumb question but I am just learning bash
Sorry for what seems like a silly question: But I've never, ever worked with
I'm sorry if this seems like a random question, but I have a database
Sorry if this seems like an easy question, but I've started pulling hair out
Sorry, this seems like such an obvious question, but I just can't find an
Sorry if this seems like a silly question - I am an amateur when
Sorry if this question seems stupid, but it's stumped me for a couple days,

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.