0

In: Decoding bitmaps in Android with the right size - it is posted that sampling down a bitmap may still lead to memory errors.

Is this issue still valid for newer versions of Android?

Does this mean that before sampling down, Android needs to load the full-size bitmap into memory? I believe this uses native memory, since image handling is implemented in native code.

Community
  • 1
  • 1
mparaz
  • 2,049
  • 3
  • 28
  • 47

2 Answers2

1

Newer versions might have more physical memory but it's still limited to whatever is the system stack size; that way, programming bitmap still should be done very carefully with the amount of memory used.

On this years Google I/O (2012) there was a great presentation named "Doing more with less" that shows some very nice caching techniques you should look into. I'm sure you can find this video on YouTube Android Developers account.

Found the video: http://www.youtube.com/watch?v=gbQb1PVjfqM

Budius
  • 39,391
  • 16
  • 102
  • 144
1

Here is how you can get the available memory amount:

Runtime.getRuntime().maxMemory()

and here is an interesting blog about memory: http://codelog.dexetra.com/getting-around-android-memory-blues

The problem with Android is ou never know when you may run ou of memory and the app just crashes. That is why you should try to avoid loading too large bimaps (I usually use maximum of 800 /600 or something like that depending on the screen orientation....)

If loading several bitmaps like for gallery or other view group you should use weak reference: http://developer.android.com/reference/java/lang/ref/WeakReference.html

Also if you look at the example in the link you provided the scaling first decodes only bounds and then decides on the scale to use.

When you don't need a bitmap anymore call:

 bitmap.recycle();
vallllll
  • 2,731
  • 6
  • 43
  • 77
  • 1
    Do not use WeakReference! One of the advice/tecnique on the video I mentioned is that WeakReference are not very good for that, even thou largely used. It points and shows code to use LruCache instead that is available in the Compatibility pack the video is here: http://www.youtube.com/watch?v=gbQb1PVjfqM – Budius Aug 15 '12 at 14:25