Android grow heap - should I worry?

2019-06-24 02:04发布

I've read something about an application having 16MB of memory "reserved" when it's on front. At least, as a developer I should count on this, even though depending on the device it may be more.

I'm supporting down to 2.2 froyo, so many devices will not be high-end devices. I get some messages in Logcat saying this:

03-29 14:08:51.570: I/dalvikvm-heap(19899): Grow heap (frag case) to 13.624MB for 200765-byte allocation

What worries me is the 13MB. I don't know if my app may crash if it reaches 16MB. Should I seriously consider optimizing some code?

2条回答
Ridiculous、
2楼-- · 2019-06-24 02:41

Android memory management is not that trivial and you should always stay as low as possible when it comes to memory usage.

Each Android device has limited amount of memory available. In most cases it's related to screen size, CPU computing power and some other (like 3D screens require twice as much memory in some cases). Nexus One has 32MB, Galaxy Nexus - 64MB, tablets can have even more. Maximum heap size can be changed in system settings. Cyanogen Mod allows quite easy adjustments.

Older Androids (before 2.3?) had this memory limit split into two parts - standard (ordinary objects, UI, Strings and so on) and multimedia (images, audio buffers, OpenGL stuff, camera frame). Nexus One with Android 2.2 had 16MB/16MB heap and it was sometimes quite irritating as you could load 16MB of images and 100KB of UI to crash an app showing simple gallery.

Some devices won't allow you to allocate too big block of memory. For example, Galaxy Nexus has limit of 31.9999MB. Also, the bigger continous block of memory you're trying to allocate, more time the system will need to complete such allocation. It has to reallocate some stuff to find such big block of memory.

Newer Androids have largeHeap flag, which gives a lot of memory. Usually somewhere around 5x more that usually. Although it sounds nice, using largeHeap is not recommended (except debugging) as it may lead to serious problems with memory usage. Basically the system needs some memory for itself. If you take more than you should, Android will kill your apps without any notice, starting from the least recently used ones.

Garbage collector may have different strategies depending on the system version and implementation. Stock Android 2.3 (I guess) on Nexus One was so aggressive that we were unable to allocate more than 13MB of images. It may look like you have a lot of memory free, and suddenly boom - your app is down.

You can use native memory. Each Android app is (more or less) a linux process, which can allocate memory for itself using direct malloc calls. You need JNI and C code, but using it you can access entire memory and do whatever you want. Again, be warned that the system will kill your app when the amount of free memory will became too low.

You can use multiple processes to split your app and divide memoty usage between them. It's not that convenient, but it works and some libraries use it to get some more resources than it should be possible at first sight.

Except that, you have to remember that some objects may use more memory, than you think. For example, hardware acceleration is a bit tricky. Most of devices don't support non-power-of-two sizes of textures. It means that when you have an ImageView (300x50px) and hardware acceleration enabled, Android will allocate a 512x64px texture for you ImageView. It has to, because everything is drawn by OpenGL and it needs textures. With such texture your ImageView takes ~2.5 times more memory than it should without hardware acceleration. Also, if your View is animated, Android has to rebuild the texture in each animation frame.

To sum up - Android can be tricky, when it comes to memory management. Heap growing to larger value may not be wrong as sometimes you need larger amount of memory (for example when creating a gallery). You should be aware that there is a lot of different things that can affect your app memory usage and try to stay as low as possible.

查看更多
一纸荒年 Trace。
3楼-- · 2019-06-24 03:00

If you really get every time the same error then, the best way is to increase the Heap size. And it is very simple to increase the Heap size.

It won't affect your other services. Increase heap size in .ini file where your eclipse stored.

查看更多
登录 后发表回答