0

i had a problem with load images as bitmap , therefore i use the method:

"decodeSampledBitmapFromFile"

(the implementation is included)

and i also saved all the bitmaps on the SdCard and every time that i need the Bitmap i load it from the Path which it's stored with the parameters:

decodeSampledBitmapFromFile(path,150,100);



Bitmap image_profile =decodeSampledBitmapFromFile(path,150,100);

and set the image Bitmap into the imageView that i need (every time that i need a image i load it from the sdCard.

however i still get an OutOfMemoryException after load about 20 images.

So, what is the solution for the OutOfMemoryException?

why even after load small number of images (about 20) i get an OutOfMemoryException?

what is the secret of application like facebook, instagram or youtube that they suceesseed

to load huge number of images without Exception?

i tried everything however i still get Exception.

anyOne has further suggestions what can i implement in order to avoid this exception?

thanks alot

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
    { // BEST QUALITY MATCH

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            int inSampleSize = 1;

            if (height > reqHeight) {
                inSampleSize = Math.round((float)height / (float)reqHeight);
            }

            int expectedWidth = width / inSampleSize;

            if (expectedWidth > reqWidth) {
                //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
                inSampleSize = Math.round((float)width / (float)reqWidth);
            }
        options.inSampleSize = inSampleSize;

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(path, options);
      }
Adir Rahamim
  • 453
  • 1
  • 12
  • 31
  • It seems to me that `BitmapFactory.decodeFile(path, options);` is leaking. Create a temp Bitmap variable here, and return this variable at the end of your method. – wangyif2 Apr 21 '13 at 20:18
  • hi, what do you mean "is leaking"? maybe can you explain more where am i wrong? – Adir Rahamim Apr 21 '13 at 20:58
  • If you have around 30 min, I would recommend watching this I/O video, it explain everything one needs to know about allocating Bitmap memory: http://www.youtube.com/watch?v=_CruQY55HOk – wangyif2 Apr 21 '13 at 21:02

1 Answers1

0

You're probably loading the images and then they're not getting recycled or collected by the garbage collector so they're still taking up memory.

Since it sounds like you're only using one image at a time, I guess you could try to manually get rid of the references to the Bitmap that you don't need anymore and then call System.gc() to free up the memory?

If not, I would look into an LruCache.

Google has a good tutorial on it: http://developer.android.com/reference/android/util/LruCache.html

This tutorial also helped me when I first used an LruCache for my game: http://andrewbrobinson.com/2012/03/05/image-caching-in-android/

corgichu
  • 2,580
  • 3
  • 32
  • 46
  • You don't have to call `System.gc()` to recycle bitmap, as long as a reference is not help, `gc` should have recycled it. The only issue might be he's allocating faster than gc could collect. – wangyif2 Apr 21 '13 at 20:19
  • @wangyif2 yeah, I should've been more clear. That's why I suggested calling `System.gc()` because it'll suggest that the garbage collection be started so it may start collecting sooner than the default and prevent the `OutOfMemoryException`. I'm not sure how much of a difference it really makes since calling `System.gc()` doesn't start the garbage collection right away and just suggests it start, but it may help. – corgichu Apr 21 '13 at 20:25
  • I think a better way to fix this error and get rid of uncertainties is the `LruCache`. – corgichu Apr 21 '13 at 20:25
  • If you're one pre-Honey you may look into calling recycle(): http://stackoverflow.com/questions/3823799/android-bitmap-recycle-how-does-it-work – britzl Apr 21 '13 at 20:40
  • hi, if i put a bitmap in imageView does the garbaeCollector clean this bitmap? or saves the bitmap? because i have alot of imageView's and i need to load a lot of bitmaps there...i think also that the LRU_Cache will not help me because i need to store there the bitmap too (therefore i think it will be the same problem). Also the LruCache is only with 12 API level or more, if i want to use it in API 8 or 10, what can i do? – Adir Rahamim Apr 21 '13 at 20:57
  • @AdirRahamim Take a look at this presentation by Google on memory management. They're pretty much building a picture gallery which would have a lot of `ImageView`s: http://youtu.be/gbQb1PVjfqM – corgichu Apr 21 '13 at 23:19
  • They use an LruCache. I think LruCaches aren't that hard to implement yourself if you can't use the Google one. They're pretty commonly used. – corgichu Apr 21 '13 at 23:19