Android增长堆-我应该担心吗? [英] Android grow heap - should I worry?

查看:70
本文介绍了Android增长堆-我应该担心吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关一个应用程序的信息,该应用程序位于前端时具有保留"的16MB内存. 至少,作为开发人员,我应该指望这一点,尽管取决于设备的情况可能更多.

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.

我支持的最低版本为2.2,所以很多设备都不会是高端设备. 我在Logcat中收到一些消息,内容是这样的:

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

让我担心的是13MB.我不知道我的应用程序达到16MB时是否可能崩溃.我应该认真考虑优化一些代码吗?

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?

推荐答案

Android内存管理不是那么简单,在内存使用方面,您应始终保持尽可能低的水平.

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

每个Android设备的可用内存量都有限.在大多数情况下,这与屏幕大小,CPU计算能力等有关(例如3D屏幕在某些情况下需要两倍的内存). Nexus One有32MB,Galaxy Nexus-64MB,平板电脑甚至可以拥有更多.可以在系统设置中更改最大堆大小. Cyanogen Mod可以轻松调整.

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.

较早的Android(2.3之前的版本)将此内存限制分为两部分-标准(普通对象,UI,字符串等)和多媒体(图像,音频缓冲区,OpenGL材质,相机框架).装有Android 2.2的Nexus One具有16MB/16MB的存储空间,有时您会感到非常恼火,因为您可以加载16MB的图像和100KB的UI来使显示简单图片的应用程序崩溃.

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.

某些设备不允许您分配太大的内存块.例如,Galaxy Nexus的限制为31.9999MB.同样,您尝试分配的更大的连续内存块,系统将需要更多的时间来完成这种分配.它必须重新分配一些东西才能找到这么大的内存块.

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.

较新的Android拥有largeHeap标志,该标志提供了大量内存.通常是通常情况下的5倍左右.尽管听起来不错,但不建议使用largeHeap(调试除外),因为它可能导致严重的内存使用问题.基本上,系统需要一些内存.如果您花了更多钱,那么从最近使用最少的应用开始,Android会在没有任何通知的情况下终止您的应用.

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.

垃圾收集器可能有不同的策略,具体取决于系统版本和实现. Nexus One上的库存Android 2.3(我想)太激进了,以至于我们无法分配超过13MB的图像.看来您有很多可用的内存,然后突然增加-您的应用程序已关闭.

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.

您可以使用本机内存.每个Android应用程序(或多或少)都是一个linux进程,可以使用直接malloc调用为其自身分配内存.您需要JNI和C代码,但是使用它可以访问整个内存并执行所需的任何操作.再次提醒您,当可用内存量变得太低时,系统将终止您的应用程序.

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.

除此以外,您必须记住某些对象使用的内存可能比您想象的要多.例如,硬件加速有些棘手.大多数设备不支持非2幂幂的纹理.这意味着,当您启用ImageView(300x50px)并启用了硬件加速后,Android会为您分配ImageView的512x64px纹理.必须这样做,因为所有内容都是由OpenGL绘制的,并且需要纹理.有了这样的纹理,您的ImageView所需要的内存将比没有硬件加速时多出2.5倍.另外,如果您的View是动画的,则Android必须在每个动画帧中重建纹理.

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.

总而言之-在内存管理方面,Android可能很棘手.将堆增长到更大的值可能不会出错,因为有时您需要更大的内存量(例如,在创建画廊时).您应该意识到,有很多不同的因素会影响您的应用程序内存使用率,并尝试将其保持在尽可能低的水平.

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.

这篇关于Android增长堆-我应该担心吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆