Android Understanding Heap Sizes Android Understanding Heap Sizes android android

Android Understanding Heap Sizes


When you set the VM budget on your emulator/device, what you are doing is telling the heap the maximum size it is allowed to be. At runtime, the heap grows dynamically in size as the Dalvik VM requests system memory from the operating system. The Dalvik VM typically starts by allocating a relatively small heap. Then after each GC run it checks to see how much free heap memory there is. If the ratio of free heap to total heap is too small, the Dalvik VM will then add more memory to the heap (up to the maximum configured heap size).

That being said, the reason why you are not seeing "24 mb" on your DDMS screen is because the heap hasn't grown to its maximum size. This allows Android to make good use of the already small amount of memory that is available on handheld devices.

As for why your application is crashing on the emulator and not your phone, that does seem odd (are you sure the numbers are correct?). You should keep in mind, however, that memory is managed dynamically and that total memory utilization is determined based on a number of external factors (the speed/frequency at which garbage collection is performed, etc.).

Finally, for the reasons I mentioned above, it would be difficult to say for sure how well your application manages memory based on the single line of information you provided above. We'd really need to see some of your code. OutOfMemoryErrors are definitely worth worrying about, however, so I'd definitely look into your application's memory usage. One thing you might consider is to sample your bitmap images at runtime with calls to inSampleSize using the BitmapFactory class. This can help reduce the amount of memory required to load your drawable bitmaps. Either that or you could reduce the resolution of your drawables (although 20 kb each sounds fine to me).


Even if your application doesn't reach the "24mb" (varies within devices) heap limit you might still have crashes because Android takes a while to grow the heap space for your app.

In my situation I was creating and dumping several images in a small amount of time.

Quite often I was getting OutOfMemoryError.

It seems Android wasn't fast enough to grow the heap space for my app.

I believe I solved this issue by using the largeHeap setting in the Manifest file.With that setting on, Android leaves more free memory every time it grows the heap, minimizing the chance of hitting the current limit.

I don't use the 24mb limit but this largeHeap conf was quite handy.

You just need to set largeHeap="true" on the application tag of your AndroidManifest.xml

<application    android:icon="@drawable/ic_launcher"    android:label="@string/app_name"    android:largeHeap="true"    android:theme="@style/AppTheme" >

Still, make sure you are carefull when dealing with images, like @Alex Lockwood advised.