Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two questions about max heap sizes and available memory in android

Tags:

I see that the Heap Size is automatically increased as the app needs it, up to whatever the phone's Max Heap Size is. I also see that the Max Heap Size is different depending on the device.

So my first question is, what are the typical Max Heap Sizes on Android devices? I have tested memory allocation on one phone that was able to use a heap over 40mb while another gave out OutOfMemory errors in the 20's mbs. What are the lowest that are in common use and what are the highest that are on common devices? Is there a standard or average?

The second question, and more important one, is how to ensure you are able to use the resources available per device but avoid using too much? I know there are methods such as onLowMemory() but those seem to be only for the entire system memory, not just the heap for your specific application.

Is there a way to detect the max heap size for the device and also detect when the available heap memory is reaching a low point for your application?

For example, if the device only allowed a max heap of 24mb and the app was nearing that limit in allocation, then it could detect and scale back. However, if the device could comfortably handle more, it would be able to take advantage of what is available.

Thanks

like image 280
cottonBallPaws Avatar asked Dec 04 '10 02:12

cottonBallPaws


People also ask

What happens when heap memory is full?

When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects. Note that the JVM uses more memory than just the heap.

Does heap size affect performance?

A too small heap size may affect performance if your system also does not have enough cores, so that the garbage collectors do compete over the CPU with the business threads. At some point, the CPU spends a significant time on garbage collection.

Is there a limit to heap size?

The theoretical limit is 2^64 bytes, which is 16 exabytes (1 exabyte = 1024 petabytes, 1 petabyte = 1024 terabytes). However, most OS's can't handle that. For instance, Linux can only support 64 terabytes of data. Note: We don't recommend you exceed 2 GB of in use JVM heap.

How can we increase the size of heap in a running process?

Just use a function like malloc() or calloc() to allocate memory dynamically. To deallocate the memory and return it to the heap, use free() . These functions will manage the size of the heap by expanding or shrinking it as needed.


Video Answer


1 Answers

Early devices had a per-app cap of 16MB. Later devices increased that to 24MB. Future devices will likely have even more available.

The value is a reflection of the physical memory available on the device and the properties of the display device (because a larger screen capable of displaying more colors will usually require larger bitmaps).

Edit: Additional musings...

I read an article not too long ago that pointed out that garbage-collecting allocators are essentially modeling a machine with infinite memory. You can allocate as much as you want and it'll take care of the details. Android mostly works this way; you keep hard references to the stuff you need, soft/weak references to stuff you might not, and discard references to the stuff you'll never need again. The GC sorts it all out.

In your particular case, you'd use soft references to keep around the things that you don't need to have in memory, but would like to keep if there's enough room.

This starts to fall apart with bitmaps, largely because of some early design decisions that resulted in the "external allocation" mechanism. Further, the soft reference mechanism needs some tuning -- the initial version tended to either keep everything or discard everything.

The Dalvik heap is under active development (see e.g. the notes on Android 2.3 "Gingerbread", which introduces a concurrent GC), so hopefully these issues will be addressed in a future release.

Edit: Update...

The "external allocation" mechanism went away in 4.0 (Ice Cream Sandwich). The pixel data for Bitmaps is now stored on the Dalvik heap, avoiding the earlier annoyances.

Recent devices (e.g. the Nexus 4) cap the heap size at 96MB or more.

A general sense of the app's memory limits can be obtained as the "memory class", from ActivityManager.getMemoryClass(). A more specific value can be had from the java.lang.Runtime function maxMemory().

like image 78
fadden Avatar answered Jan 03 '23 02:01

fadden