Sanal Varghese
Sanal Varghese

Reputation: 1486

Maximum Heap size for Nexus?

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

Upvotes: 2

Views: 3945

Answers (3)

Ankit Purwar
Ankit Purwar

Reputation: 522

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

Upvotes: 1

Morrison Chang
Morrison Chang

Reputation: 12121

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.

Upvotes: 0

minhaz
minhaz

Reputation: 4233

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.

Upvotes: 0

Related Questions