Shahbaz Pothiawala
Shahbaz Pothiawala

Reputation: 1215

Images Memory Management in Android

This is one of the most asked question by beginners but still I couldn't get any help unfortunately. In an activity I have a viewflipper to which I assign imageviews with images programatically ( around 100 images added to viewflipper using for loop ). Might be due to image size or due to the large no of images I get OutOfMemory Error for bitmaps. My Questions are:

  1. If this is because my drawables take memory space, is there any way by which I can release the space occupied by them?
  2. Can garbage collection be the reason for this? If so what's the solution?
  3. Since am using a viewflipper, is there any way by which I can only load my current, previous and next view of viewflipper? Will this help occupy less memory?

Please let me know if my question is confusing or is not understandable. This is an important topic to me as I am facing this problem in almost all my apps.

Upvotes: 1

Views: 1783

Answers (3)

Jeffy Lazar
Jeffy Lazar

Reputation: 1923

Now what happens over here is every app in android is alotted a specific amount of heap size that is as little as 20mb

Bitmaps take up a lot of memory, especially for rich images like photographs

so whenever you load a heavy image or array of heavy images it exceeds the heap size and android automatically shuts down the app because it was using more than the allotted heap size

Read Documentaion

So whenever you are working with heavy images

  • Load a scaled down version
  • Asynchronously process the image
  • and then final display it in an imageview

Upvotes: 0

Hamad
Hamad

Reputation: 5152

to avoid,aquire large heap using this in manifest:

android:largeHeap="true"

and almost all problem solutions are present on these links related to memory out of error,hope that one of them may solve your problem:

meomry-out-of-error

Memory Leak and Out of memory Error using List,LinkedList and HashMap

Memory Leak and Out of memory Error using LRU Cache

Memory Leak and Out of memory Error using Disk LRU Cache

Upvotes: 4

Gil Moshayof
Gil Moshayof

Reputation: 16761

Bitmaps are handled by native memory. This means that when the GC collects the bitmap's reference, the bitmap's memory is not immediately removed. To clear the bitmap's memory immediately, you can call bitmap.recycle(). As for the ViewFlipper, you going to want to find a way to manually unload and reload images as they are displayed to the user. Remember to use recycle(), and don't try to display a bitmap that already was recycled.

Hope this helps :)

Upvotes: 1

Related Questions