Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable android:largeHeap in Android 4, and disable it in Android 2.3

Tags:

Currently, I have a piece of code, which is designed to run both in Android 2.3 and 4+

The code will perform much better (Where it will not have OutOfMemory exception most of the time), if android:largeHeap is being applied in AndroidManifest.xml.

<application
    android:name=".MyApplication"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:screenOrientation="nosensor"
    android:largeHeap="true"

Currently, my android:minSdkVersion need to set to 15 instead of 10 (Android 2.3). If not, android:largeHeap is not allowable in AndroidManifest.xml.

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="15" />

Within single APK, how possible I can set

  • Use android:largeHeap option if I were in Android 4+
  • Do not use android:largeHeap option if I were in Android 2.3
like image 573
Cheok Yan Cheng Avatar asked Feb 17 '13 04:02

Cheok Yan Cheng


People also ask

What is largeHeap in android manifest?

Setting the optional android:largeHeap="true" flag in the AndroidManifest. xml file allows your application's processes to be created with a large Dalvik heap. PDF document rendering can be memory intensive and this flag is recommended to ensure that your application has enough heap memory allocated.

What is large heap?

Actually android:largeHeap is the instrument for increasing your allocated memory to app. There is no clear definition of the need to use this flag.


1 Answers

You can also disable large heap in Honeycomb and enable it in ICS or JB. Just a little hacky or something. Here's what I tried.

Before we proceed, change your Build target to Honeycomb, Jelly Bean or ICS so we can put android:largeHeap attribute. Also, you can set android:minSdkVersion to API 10.

Android API 10 doesn't support large heap.

  1. Create a folder values-v14 in res folder
  2. I created bools.xml in values-v14
  3. Put this value in bools.xml of values-v14

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

boolean value for values > bools.xml or values-[API VERSION] > bools.xml to disable large-heap in different API Version or by default.

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

Change the value of android:largeHeap to @bool/largeheap instead of hardcoded true or false

<application
        android:largeHeap="@bool/largeheap"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher">
                ....
</application>

I tested this code by making a memory leak application or just load a Huge bitmaps, and, its working!

Good Luck!

like image 80
13 revs Avatar answered Sep 21 '22 14:09

13 revs