7

I would like to increase the android virtual device map heap size with Eclipse. I tried to set the Max VM application heap size to 128 in the Eclipse AVD Manager, but it does not work, the line Runtime.getRuntime().maxMemory()/(1024*1024); always returns 48, no matter the set heap size. Device ram size is set to 512. Selected target is Android 4.1.2 (API Level 16). Moreover, I have set android:largeHeap="true" in the manifest file.

Is there a limit to max heap size (is 128MiB to much ?), or is there another file to edit or parameter to set ?

trincot
  • 317,000
  • 35
  • 244
  • 286
Fitz
  • 570
  • 1
  • 5
  • 23
  • read answer on this [question](http://stackoverflow.com/questions/2131947/android-memory-allocation) – Bishan Jan 09 '13 at 09:59

4 Answers4

3

add large heap in Manifest file

<application

        android:icon="@drawable/example"
        android:label="@string/app_name" 
        android:largeHeap="true">

      ......
      ......

</application>
ShreeshaDas
  • 2,042
  • 2
  • 17
  • 36
2

To do that, in Eclipse, go to "Debug Configurations". You can find that in the drop-down under the "debug" icon. Select "target", and select a preferred emulator target to launch. Then under "additional emulator command line options," add this:

-partition-size 128

Then CLOSE the emulator (and remove any devices), and click the debug icon, which will launch the preferred emulator you selected. This is important: Eclipse needs to launch the debugger, not AVD.

Note that the size (128) is in megabytes.

Take a look also here: http://viralpatel.net/blogs/jvm-java-increase-heap-size-setting-heap-size-jvm-heap/

Or: increase the AVD RAM and the max VM application heap size in VM options: Go to Window-->AVD Manager-->Virtual Devices-->Edit.

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
  • I tried to add the -partition-size parameter in the command line options, but result remains the same : the max heap size appears to be 48 MiB. And I had already set the AVD options, but without effects. – Fitz Jan 09 '13 at 10:08
  • Ok, and what about the VM heap size (last point of my answer.) – Milos Cuculovic Jan 09 '13 at 10:09
  • Hm, strange. Are you sure that you are getting the right value when reading the max memory? – Milos Cuculovic Jan 09 '13 at 10:10
  • 1
    The `Max VM application heap size` parameter is set to 128 and the `Device ram size` is set to 512. But `Runtime.getRuntime().maxMemory()` still returns 48 MiB. I use `Log.v("Utils", "Max mem in MB : " + (Runtime.getRuntime().maxMemory()/(1024*1024)));` to get the max memory. I guess it should return the good value ? – Fitz Jan 09 '13 at 10:11
  • 1
    You can also set the min heap size of the vm: java -Xms64m -Xmx256m HelloWorld – Milos Cuculovic Jan 09 '13 at 10:12
  • As I said, I have set `Max VM application heap size` parameter to 128 and the `Device ram size` to 512 by editing the AVD. But it does not seem to have effect. – Fitz Jan 09 '13 at 10:37
  • It can be that the AVD will choose the heap size according to the needs of your app. What if you set the max heap size to be 24. You will probably see 24 instead of 48 in your log. – Milos Cuculovic Jan 09 '13 at 10:43
  • I really don't understand why, but when I set the max heap size to be 24, the logger still says it is 48. NB: I restart the emulator every time I set new options for the AVD. – Fitz Jan 09 '13 at 10:58
  • That's really strange. It should change, specially when you are seting the heap size in avd to be less than the 48 you have. I am not sure at all but probably (to be tested), you are not accessing the right variable when checking the heap size. – Milos Cuculovic Jan 09 '13 at 11:01
  • It might be that, but actually I need to run an application that require a lot of memory and it crashes with an OutOfMemory exception when memory allocation exceed 48MiB. – Fitz Jan 09 '13 at 11:35
  • Even if you set 128Mb ? Be carrefull with this type of applications, usually, real mobile devices are running with 24 / 32 M of memory, and tablets with 64MB. So you risk to have OutOfMemory exception on a real device, which is bad. You should fiyx your app before releasing it. – Milos Cuculovic Jan 09 '13 at 12:29
1

Use the following code. I think it would be help you:

VMRuntime.getRuntime().setMinimumHeapSize(4 * 1024 * 1024);
Android Boy
  • 4,335
  • 6
  • 30
  • 58
1

you can't increase the heap size dynamically.

you can request to use more by using android:largeHeap="true" in the manifest, but you might not get any more heap size than normal, since it's only a request.

also, you can use native memory, so you actually bypass the heap size limitation.

here are some posts i've made about it:

and here's a library i've made for it:

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