3

I'd love to use the Android:LargeHeap="true" option in the Manifest to get a bit extra memory (we're dealing with 5+ MB bitmaps on high-res 1980x1200 displays and expect even larger displays shortly.

I've run out of all the normal tricks to dealing with the poor memory handling in Android for bitmaps (i.e. there is no way to know if there is a hole large enough in fragmented memory other than to try and pray it doesn't crash). I've already spent weeks optimizing, trimming and applying other tricks to minimize memory and prevent crashes. It's a trick of what features can be done in 2.x vs 3.x/4.x, yet keep it all in a single app. No need to point to a "how to optimize for bitmaps" - I've already gone through these and applied what I could.

LargeHeap is not supported on version 2.x, and I use different images for lower res screens that do not need the LargeHeap option in 2.x. (no out of memory issue either).

When android:minSdkVersion="8", it will not allow the Android:LargeHeap option at all.

Is there any way to conditionally include LargeHeap for systems with 3.x, and have it ignored for 2.x? Or have the application itself attempt to set LargeHeap if 3.x is detected? I can't find any way to do this, but perhaps I've overlooked some trick.

I also realize LargeHeap is rather awful, but we are running out of other tricks. Ideally, it would be nice to do LargeHeap programatically, only when really needed (and allowed) at the program OnCreate.

Frank
  • 605
  • 1
  • 8
  • 14

3 Answers3

5

When android:minSdkVersion="8", it will not allow the Android:LargeHeap option at all.

Yes, it will.

Now, if your build target (e.g., Properties > Android) is set to API Level 8, it won't compile. You will need to compile with a build target API Level 11 or higher. But android:minSdkVersion has nothing to do with it. The option will simply be ignored on older devices.

Is there any way to conditionally include LargeHeap for systems with 3.x, and have it ignored for 2.x?

Use android:largeHeap="true" in the <application> element in the manifest.

Or have the application itself attempt to set LargeHeap if 3.x is detected?

AFAIK, there is no programmatic means to set this.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
5

I use this method :

in AndroidManifest.xml

android:largeHeap="@bool/largeheap"

And I create 2 configuration.xml file

The first in values : configuration.xml

<bool name="largeheap">false</bool>

The 2nd in values-v11 (use only with version >= 11)

<bool name="largeheap">true</bool>
Anthone
  • 2,156
  • 21
  • 26
0

if you handle a lot of large bitmaps, you'd better work with JNI, which isn't constrained by heap limitations , but by the device RAM itself.

another approach is to be flexible according to how much available heap size the device has.

as far as I know, using the largeHeap flag is ok, but you don't get any promise as to how much more heap size you get, if at all . in fact, i'm not sure if google even has a requirement from the manufacturers as to how much more it should give you.

in order to use the largeHeap flag, just set it in the manifest, and use targetSdk to be set to the latest one available. in the minSdk you set the minimal API that you support, so this flag would be automatically ignored when running on old devices. same goes for any xml attribute that belongs to new APIs.

only classes and methods that belong to newer APIs would cause you problems, but for this you should read Lint's warnings .

android developer
  • 114,585
  • 152
  • 739
  • 1,270