6

When I change to landscape mode, few objects are created with bitmap of full screen.

When I scroll the other object is called and its bitmap is displayed, when I doing this repeatedly , bitmap size exceeds vm budget error, I have done all the things like recycle(), set null and then called GC(), still I have same error. creating bitmap....

    bitmap = Bitmap.createBitmap(ChartProperties.getChartWidth(), 
                    ChartProperties.getChartHeight(),
    Bitmap.Config.RGB_565);

    imageCache.put(String.valueOf(LandscapeChartActivity.getActiveFeature()),
                    new SoftReference(bitmap));

    if(imageCache != null){

        for (int i = 0; i < imageCache.size(); i++) {

            if (imageCache.get(String.valueOf(i)) != null) {
                imageCache.get(String.valueOf(i)).get().recycle();
                imageCache.put(String.valueOf(i), null);                    
            }

        }
        Runtime.getRuntime().gc();
        imageCache.clear();
        imageCache = null;
wythagoras
  • 316
  • 8
  • 16
Mihir Shah
  • 1,799
  • 3
  • 29
  • 49
  • possible duplicate of [OutOfMemoryError: bitmap size exceeds VM budget :- Android](http://stackoverflow.com/questions/2928002/outofmemoryerror-bitmap-size-exceeds-vm-budget-android) – Sergey Glotov May 24 '12 at 12:58
  • 2
    Every time you call `size()` in a loop, God kills a kitten... – WarrenFaith May 24 '12 at 13:09

1 Answers1

7

I also had the same problem OOME because of bitmaps.

When orientation changes from PORTRAIT to LANDSCAPE and vice-versa, the previous UI is completely discarded, and a new UI is loaded and displayed, In this case if you are using many bitmaps in your app, you need to release them at proper places.

To check the orientation of your device, please see this: Check orientation on Android phone

In your case, you need to clear bitmaps during orientation change.

On above link you can found, how to get the current orientation. So on each orientation change, call your above code that cleans up the bitmaps.

Now, when we check the logcat, there is always a log comes up saying GC_, but I could not understand that, so I found an amazing doc on memory leak issue: http://codelog.dexetra.com/getting-around-android-memory-blues

The above link is very useful for your problem.

Now, the OOME occurs when there is memory leak in your app., so to check that, please install the MAT for eclipse. You can find it at: http://www.eclipse.org/mat/downloads.php

Its a bit complicated software but as you go through it, you will understand, its pretty useful software.

Even if this doesn't solves your problem, use the WeakReference for bitmaps.

Please refer this link: How to use WeakReference in Java and Android development?

If I get know some more info, I will update this post.

Please update your post, if you get solution to your problem.

Thank you :)

Community
  • 1
  • 1
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
  • Thank you, But in my code i already clear bitmaps while orientation change, but still error occur when flip many times..if(imageCache != null){ for (int i = 0; i < imageCache.size(); i++) { if (imageCache.get(String.valueOf(i)) != null) { imageCache.get(String.valueOf(i)).get().recycle(); imageCache.put(String.valueOf(i), null); } } Runtime.getRuntime().gc();`enter code here` imageCache.clear(); imageCache = null; – Mihir Shah May 25 '12 at 08:34
  • There is certainly a memory leak in your app, please install MAT for eclipse. You will come to know exact place where memory leak is present so that you can rectify it. – Shrikant Ballal May 25 '12 at 09:08