How to work around Android’s 24 MB memory limit

转载 2012年10月25日 10:42:41

The Android framework enforces a per-process 24 MB memory limit. On some older devices, such as the G1, the limit is even lower at 16 MB.

What’s more, the memory used by Bitmaps is included in the limit. For an application manipulating images it is pretty easy to reach this limit and get the process killed with an OOM exception:

E/dalvikvm-heap(12517): 1048576-byte external allocation too large for this process.

E/GraphicsJNI(12517): VM won't let us allocate 1048576 bytes

D/AndroidRuntime(12517): Shutting down VM

W/dalvikvm(12517): threadid=1: thread exiting with uncaught exception (group=0x4001d7f0)

E/AndroidRuntime(12517): FATAL EXCEPTION: main

E/AndroidRuntime(12517): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

This limit is ridiculously low. For a device, like the Nexus One, with 512MB of physical RAM, setting the per-process memory limit for the foreground activity to only 5% of the RAM is a silly mistake. But anyway, that’s how things are and we have to live with it — i.e. find how to work around it.

There are two ways to allocate much more memory than the limit:

One way is to allocate memory from native code. Using the NDK (native development kit) and JNI, it’s possible to allocate memory from the C level (e.g. malloc/free or new/delete), and such allocations are not counted towards the 24 MB limit. It’s true, allocating memory from native code is not as convenient as from Java, but it can be used to store some large amounts of data in RAM (even image data).

Another way, which works well for images, is to use OpenGL textures — the texture memory is not counted towards the limit.

To see how much memory your app has really allocated you can use android.os.Debug.getNativeHeapAllocatedSize().

Using either of the two techniques presented above, on a Nexus One, I could easily allocate 300MB for a single foreground process — more than 10 times the default 24 MB limit.

相关文章推荐

Android Memory Management, OutOfMemoryError

A    Android框架强制每个进程的24 MB内存限制。在一些旧的设备,如在G1,限制为16 MB 更低,更重要的是,由位图使用的内存限制。处理图像的应用程序,它是很容...

Android签名机制之---签名验证过程详解

今天是元旦前夕,也是Single Dog的嚎叫之日,只能写博客来祛除寂寞了,今天我们继续来看一下Android中的签名机制的姊妹篇:Android中是如何验证一个Apk的签名。在前一篇文章中我们介绍了...

android签名机制

1.android为什么要签名

旋屏时,OnCreate方法重复调用的解决

Android设备屏幕自动横竖旋转时,每次都会重新调用onCreate函数进行初始化操作,如果遇到加载数据量稍微大点的时候就需要等待很长时间,用户体验较差,其实可以通过配置AndroidManifes...

Gradle build报错:Please correct the above warnings first解决方案

转载:gradle build报错:Please correct the above warnings first解决方案问题描述:在编译的时候,出现了下面的错误提示Warning: there we...

android系统信息(内存、cpu、sd卡、电量、版本)获取

要转载请注明出处:http://gqdy365.iteye.com/blog/1066113,有很多转载了文章不写出处,还写的是什么小编最近做项目碰到什么问题怎么解决的的然后把文章贴下面,俨然一副他们...

Android APK 签名机制

发布过Android应用的朋友们应该都知道,Android APK的发布是需要签名的。签名机制在Android应用和框架中有着十分重要的作用。例如,Android系统禁止更新安装签名不一致的APK;如...

android 弹出窗口

正常的Dialog弹出窗口,当点击窗口外围时关闭弹出的窗口,这个在很多弹出式的Activity上很为实用!   dialog.xml 弹出窗口的界面布局:   view p...

android apk 解包、打包、签名等一些操作的命名指导

首先下载apk解包、打包、签名所要的工具 http://download.csdn.net/detail/changcsw/6741573 下载后的压缩包中包含 解包、打包、签名 所用到的所以工具 ...

Android签名机制之---签名过程详解

一、前言又是过了好长时间,没写文章的双手都有点难受了。今天是圣诞节,还是得上班。因为前几天有一个之前的同事,在申请微信SDK的时候,遇到签名的问题,问了我一下,结果把我难倒了。。我说Android中的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)