2

I am using Galaxy Nexus(i9250) for development and testing. I noticed strange fact- sometimes when the total heap size is 64mb and allocated heap size is in and around 56-60mb, app crashes. But sometimes I noticed that even the memory shoots up to 80mb, app didn't crashes.

Initially I thought that maximum heap size for devices of the range nexus will be 64mb(now I realize it is wrong). So my question is what is the maximum heap size a device can use. If it is variable based on device, then on what factor heap size depends. I knew this is a common question. Could anyone guide me to the right answer. Thanks in advance!

NOTE: I didn't use LargeHeapSize = true; in my code

onemach
  • 3,847
  • 5
  • 32
  • 47
Sanal Varghese
  • 1,395
  • 4
  • 20
  • 46
  • 4
    What are you doing that requires that much heap space? – Morrison Chang Apr 07 '13 at 19:40
  • 5
    I know this is nitpicky, but I find it incredibly annoying when people aren't specific about something they need to specific about. There is the Nexus One, Nexus S, Galaxy Nexus, Nexus 4, Nexus 7, and Nexus 10. You've told us you are using the "Nexus", you might as well tell us you are using an "Android". I'm assuming this is a 7 or a 10, but cmon man... – Steven Byle Apr 07 '13 at 19:54
  • http://stackoverflow.com/questions/5350465/android-heap-size-on-different-phones-devices-and-os-versions – dongshengcn Apr 07 '13 at 20:07
  • @StevenByle: I am talking about galaxy nexus. – Sanal Varghese Apr 07 '13 at 20:14

3 Answers3

1

Considering , your app crashes every time giving "OutOfMemoryError"

DVM starts by allocating a small heap, after every garbage collection cycle it checks for free heap and compares it to the total heap memory and adds more memory in case that the difference is too small. So Heap can be increased or decreased at runtime as per Dalvik VM's requirement by OS.

So, when their is enough memory available in the system your app didn't get crashed when the memory shoots up to 80mb.(Assuming 64MB is not the hard limit of heap size)

Yes there is a hard limit on the heap size of every device which can be increased by using "LargeHeapSize = true" , but this might be too costly in terms of app performance as increased heap size is directly proportional to the time taken by the garbage collection process(as GC now have to traverse more objects inside bigger heap). So it is a big "NO,NO" until unless you exactly know what and why are you going for larger heap size.

on what factor heap size depends:

  • Heap size mainly depends on the resolution of the device you are using, as more resolution needs larger images/bitmaps size to fit in.(more pixels = needs more memory to incorporate)

  • Although, I didn't got through any written proof of this.. As per my understanding Heap size also depends on the RAM size. Since, bigger
    RAM size will allow more flexibility of multitasking and lesser
    chances of getting the "OutOfMemoryError"

In case you needs to know exact amount of heap memory you can use, ActivityManager.getMemoryClass(). This method returns an integer indicating the number of megabytes available for your app's heap

ActivityManager.getLargeMemoryClass() for larger heap size

Ankit Purwar
  • 472
  • 5
  • 17
0

it is not clear that what device you are exactly talking about. It is also not clear how you calculated your heap memory.

I recommend you calculate your heap memory and available memory using this link

But if your app uses Native Memory, their are no restriction on that link.

Community
  • 1
  • 1
minhaz
  • 4,112
  • 3
  • 29
  • 49
  • I am using nexus i9250(galaxy nexus). I didn't calculated heap size rather i am talking about the heap size shown by Eclipse(you can see it in DDMS). – Sanal Varghese Apr 07 '13 at 20:11
0

I would only use the DDMS values as a guide to find memory leaks and memory allocation problems rather than some specific number given that you can target. Any Android application will be expected to run on a variety of devices so while you may have tuned your app for a 'Galaxy Nexus' you will want to be able to run on older devices, and test appropriately. See @dongshengcn comment.

In addition to the links by @minhaz I would also read: Understanding Heap Size.

If you are trying to get a better understanding of memory management on Android then you should read Android Framework Engineer @hackbod's answer to: How to Discover Memory Usage of My Application in Android.

Community
  • 1
  • 1
Morrison Chang
  • 10,296
  • 3
  • 32
  • 60