Android hashmap clear onTrimMemory doesnot releases memory

373 views Asked by At

I have a hashmap of Bitmap , which provides the adapterview with required thmbnails of images to show.

My thumbnail size is likely within 64x64 dp box.

But still i see logs like heap grown for alloction of 1.xx mb. Then i printed the bytecounts, and verified that the code i use doesn't do actually thumbnailing. The resulting bitmaps are of size mostly greater than megbytes.

The code i use is as below

@Override
        protected Bitmap doInBackground(Uri... params) {
            data = params[0];
      //      Log.e(TAG,"Async Task Drawable doInBackground");
            try {
                Bitmap bmp;
                if (MainActivity.thumbCache.containsKey(albumId)) {
                    bmp = (Bitmap) MainActivity.thumbCache.get(albumId);
                    Log.e("BMWT-HM-Size","getting from Cache"+String.valueOf(MainActivity.thumbCache.size()));
                    Log.e(TAG,": bmp.getByteCount() "+bmp.getByteCount());
                } else {
                in = res.openInputStream(data);
                songArtOptions.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(in,null,songArtOptions);
                Log.e(TAG,String.valueOf(songArtOptions.outWidth)+"x"+String.valueOf(songArtOptions.outHeight)+": "+songArtOptions.outMimeType);
                songArtOptions.inSampleSize = calculateInSampleSize(songArtOptions,songArtWidth,songArtHeight);
                Log.e(TAG,"subSampleLevel = "+String.valueOf(songArtOptions.inSampleSize));

                in = res.openInputStream(data); 
                songArtOptions.inJustDecodeBounds = false;
                bmp =  BitmapFactory.decodeStream(in,null,songArtOptions);

                MainActivity.thumbCache.put(albumId, bmp);
                Log.e("BMWT-HM-Size","newly decoded"+String.valueOf(MainActivity.thumbCache.size()));
                Log.e(TAG,": bmp.getByteCount() "+bmp.getByteCount());
                }
                return bmp;
            } catch (FileNotFoundException e) {
                return defaultArt; 
            }
        }



public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    int inSampleSize = 1;

        if (options.outHeight > reqHeight || options.outWidth > reqWidth) {

            int halfHeight = options.outHeight >> 1;
            int halfWidth = options.outWidth >> 1;

            // Calculate the largest inSampleSize value that is a power of 2 and
            // keeps both
            // height and width larger than the requested height and width.

            while (halfWidth > reqWidth && halfHeight > reqHeight) {
                inSampleSize <<= 1;
                halfWidth >>= 1;
                halfHeight >>= 1;
            }
        }

    return inSampleSize;
}

As a result, my heap grows as much images are put into the hashmap.

in OnTrimMemory I call, thumbCache.clear,which i hoped to release the occupied memory by the elements of hashmap, but it doesn't. The heap status stays the same.

How to clean this out. I want to maintain cache as long as the view is visible and want to clear the cache(which i mean releasing the occupied memory to be GC'ed) whne view is fully destroyed.

1

There are 1 answers

2
jobcrazy On

If you are sure the Bitmap has no further uses, you can call Bitmap.recycle() to reclaim memory after you remove it from your cache.