-3

Why 1 or 2gb RAM is needed in android device if a single application can use only 32mb-heap? I cannot load big images into RAM, for example 3000x3000 (it is only 34mb).

Garry
  • 4,249
  • 3
  • 25
  • 47
rint
  • 225
  • 1
  • 2
  • 10
  • 1
    you can add android:largeHeap in AndroidManifest for Application tag, but better practice is to scale images (also you can check http://developer.android.com/reference/java/nio/MappedByteBuffer.html) So show user scaled image but in background you can process full image – Andrew V. May 07 '15 at 13:17

2 Answers2

1

Available, here, in the android documentation, using

android:largeHeap="true"

will allow you to use a larger heap size but it is advised that you, instead, use smaller images if it is possible.

Whether your application's processes should be created with a large Dalvik heap. This applies to all processes created for the application. It only applies to the first application loaded into a process; if you're using a shared user ID to allow multiple applications to use a process, they all must use this option consistently or they will have unpredictable results. Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.

To query the available memory size at runtime, use the methods getMemoryClass() or getLargeMemoryClass().

Wayne
  • 341
  • 1
  • 13
  • But what about android 2.3.3? It does not support largeHeap – rint May 07 '15 at 14:12
  • Android 2.3.3 has such a small percentage compared to later releases that it's virtually pointless to support said version. The last check that I, myself, had performed indicated that approximately 3% of all android users were on version 2.3.3 or earlier.. In addition, images the size of 3000x3000 would take far too long to load/render on devices that run indicated versions of android. – Wayne May 08 '15 at 03:50
0

Yes, this is true. Some might argue that this poses one of the biggest challenges when developing applications for Android, especially those which need to load large images.

The best way to overcome this, is to simply accept this limitation when working with Android application, and be very careful when creating Bitmaps. In principle, you should never have more Bitmaps in memory than can be displayed on a screen at one time. This means loading smaller Bitmaps, and recycling Bitmaps which are no longer in use.

Sometimes however, you need to hold lots of Bitmaps at once, and this amount exceeds the memory limitation each app is given.

For this, perhaps the best option, would be to render these Bitmaps as OpenGL-ES textures.

When you use OpenGL-ES in Android, textures (which is essentially the OpenGL name for Bitmaps) are saved on the device's heap, and are not limited by the same memory limit as normal Bitmaps. Here, you can take full advantage of the devices RAM.

OpenGL-ES is considered a slightly more "advanced" topic for developers, but if you follow a few tutorials such as this one, you can set something basic up pretty quickly.

As a final note, you can also define in your manifest file largeHeap="true". This will allocate your app with a bit more memory, but ultimately, you will end up with the same problem as before - no access to the full device memory. It's a good fix if you just need a little more memory, but if your memory requirements are very large, then it probably won't be enough.

Gil Moshayof
  • 16,053
  • 4
  • 41
  • 54