Sunday, September 27, 2009

Memory limit in Android

It has come to my attention that Android applications have a heap limit of 16MB. Normally this won't pose a problems to ordenary applications, but I have found that it isn't as hard as one would think to reach this limit, especially when you are doing resource intensive things like testing. When the heap size reaches above 16MB you will get an java.lang.OutOfMemoryError exeption.
It is posible to increase this limit, but it seem to require direct intervension into the the Android source. By defining a compile flag, CUSTOM_RUNTIME_HEAP_MAX one can set the parameter that defines the max heap size of dalvik. This flag will then be used by the AndroidRuntime::start method in the frameworks/base/core/jni/AndroidRuntime.cpp to define the -Xmx parmater that sets the max heap size of dalvik.

ifdef CUSTOM_RUNTIME_HEAP_MAX
#define __make_max_heap_opt(val) #val
#define _make_max_heap_opt(val) "-Xmx" __make_max_heap_opt(val)
opt.optionString = _make_max_heap_opt(CUSTOM_RUNTIME_HEAP_MAX);
#undef __make_max_heap_opt
#undef _make_max_heap_opt
#else
/* limit memory use to 16MB */
opt.optionString = "-Xmx16m";
#endif

So if you define CUSTOM_RUNTIME_HEAP_MAX with a value like "20m" you will have increased the maximum heap size to 20MB.

21 comments:

  1. Hi there I am very new to java and I have an incredibly basic question. where do i input this code? does this string need to go into my application or is there some other way to invoke this?

    ReplyDelete
  2. First the code above is taken from the android source code, so changing this will involve changing the Android source code and then recompiling it. Applications on Android does not have the option of changing their heap size. If you decide to recompile Android yourself, you could set "CUSTOM_RUNTIME_HEAP_MAX" to the limit you would like. Needless to say this quite a lot of work, but if you have it a go you can find the source code here http://source.android.com/

    ReplyDelete
    Replies
    1. Hello Tord,

      May I ask you how to recompile the whole source code?
      I am using Samsung open source code (Gingerbread), and trying to customize the dalvik heap size as you mention above.

      Thanks

      Delete
    2. Someone says that : "As of API 11 , it's possible to have more heap size by adding the "android:largeHeap = 'true' into your manifest file. In some devices , the heap size may increase to 256 MB. For API 10 and older , it's unfortunately impossible to increase the heap size since it's a system variable."

      So, can CUSTOM_RUNTIME_HEAP_MAX increase the dalvik heap size for API 10 and older???

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. so how do I change the heap limit before I compile from source? I found the -Xmx line in AndroidRuntime.cpp should I just change the 16 to what I want? what does the rest of that code do? do I add that in AndroidRuntime.cpp or do I add that to the makefile?

    ReplyDelete
  5. you could change the 16 to what you want, it would probably be the easiest thing to do. Or you could define the CUSTOM_RUNTIME_HEAP_MAX macro in the makefile. Not sure what you mean by the rest of the code.
    Hope this helps.

    ReplyDelete
  6. I want to use a huge data arrays (for example 30MB) in Java code (I want to develop a game and I need some buffers for terrain, data, ect.).

    Can I change the heap size in C++ definition files (for my current application)?

    ReplyDelete
  7. No, I'm afraid you can't change the heap size for the current application, sorry. The heap size remains the same for all applications.

    ReplyDelete
  8. Do you by chance have any idea of how I could "emulate" low memory on android? I'm trying to see if my onPause/onRestart works correctly and then I have to suffer from low memory so that android will kick out my app from memory.

    A second question as well, do you know where in the android source I can follow what happens when the gc trashes apps when the memory is low?

    Thanks!

    ReplyDelete
  9. how do i change the android source code using eclipse?

    ReplyDelete
  10. Hi, If i change max heap size to say 25MB by the method described above. that applies to particular phone right ? it can't happen to other devices if i send them .apk of app ? right? 'm i rightly understood ?

    ReplyDelete
  11. Thanks for your post.

    ReplyDelete
  12. Hello some one Please help me...

    i am taking heavy data response from server ,
    Taking server res code 200 but facing out of memory exception Please help me...

    ReplyDelete
  13. Hi ....
    My question is
    What is the CPU limitation of android?

    ReplyDelete
  14. Great post.. CUSTOM_RUNTIME_HEAP_MAX macro was what I was looking for. Thanks!

    ReplyDelete
  15. As of API 11 , it's possible to have more heap size by adding the "android:largeHeap = true" into your manifest file. In some devices , the heap size may increase to 256 MB. For API 10 and older , it's unfortunately impossible to increase the heap size since it's a system variable.

    ReplyDelete
  16. We are struggling to do our daily chores & we should be thankful for these best bible study apps for helping us to nurture & protect our faith in Lords top 10 Best Bible Study apps

    ReplyDelete
  17. List of Best Online Share Trading Apps in India. Best Stock Trading Apps. Below, I have listed the best mobile app for trading in India.
    best trading app in india

    ReplyDelete