0

I have in my application many bitmaps. They are loaded when application start. In one activity I have a two buttons where I change language, but when I click on button I start loading images again. Sometimes I get error with out of memory. How I can clean memory, that when I click on button I clean memory and after that loading bitmaps? This is possible to do? Now I use system.exit(0) but I don't want close all application.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
user1302569
  • 7,131
  • 13
  • 46
  • 66

3 Answers3

4

I was facing the same issue with a graphics-processing intensive app a while ago and after lots of debugging I figured out that Bitmap objects are not properly freed automatically.

You should manage Bitmaps on your own by calling their recycle method as soon as you don't need them anymore (Activity.onStop method for example).

EDIT (10/08/2014):

The Android developer documentation finally contains clear explanations for these bitmap memory problems. Some things have changed in the meanwhile for the better, but we typically still have to deal with older Android versions that make bitmap memory management difficult.

Here are the key points:

As of Android 2.3.3 (API level 10) and lower

  • pixel data is stored in native memory...
  • ...and not in the Dalvik heap memory
  • pixel data is released in an unpredictable way

The problem shows up when you already depleted the Dalvik heap memory limit with bitmaps (or other big objects) and try to load another bitmap. Even if you do not hold any references anymore on older bitmap objects, it is not guaranteed that those bitmaps get garbage collected before a new bitmap object is allocated. Therefore you can randomly hit the limit and get an OutOfMemoryError.

It is therefore important to manage bitmap objects yourself. When you are done with using a bitmap you should call its recycle() method before loading another bitmap.

Since Android 3.0 (API level 11)

  • pixel data is stored on Dalvik heap together with the bitmap object

Here Dalvik's memory manager can see how much memory is left and has full control over reclaiming memory.

Managing Bitmaps

In both cases (older Android versions and newer) you may want to manage your bitmap objects in order to avoid:

  • Repeated loading of the same image (optimization issue)
  • Running out of memory (stability issue)

The Android developer documentation contains detailed information about how to cache and reuse already loaded bitmap objects using a least-recently-used cache (LruCache).

tiguchi
  • 5,392
  • 1
  • 33
  • 39
1

Dont call System.exit(0) because it will terminate your JVM. Call bitmap.recycle() on every bitmap after you are finished using them. That will free the native memory acquired by the bitmap. Also setting the bitmap references to null will make the GC collect it next time its run.

This answer will help you : Catching OutOfMemoryError in decoding Bitmap

Community
  • 1
  • 1
Ron
  • 24,175
  • 8
  • 56
  • 97
1

You would not be getting OutOfMemory exceptions if the unused bitmaps were not referenced from your data structures and recycle was properly called. As soon as you ensure this, the automated garbage collector will start serving you reliably and you don't need to tell it when to free stuff.

Watch out for any static variables in you program, especially collections or complex structures. If a bitmap was added anywhere, make sure that it is removed from there again.

Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75