How to work around Android’s 24 MB memory limit
来源:互联网 发布:识别身份证真假软件 编辑:程序博客网 时间:2024/05/16 10:47
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.
- How to work around Android’s 24 MB memory limit
- How to increase MySQL memory limit?
- How to work with MeshLab's mesh
- [知其然不知其所以然-30] How to work around when system can not be woken up
- How SKB's work?
- How to work
- HashMap how to work
- How to work around a possible XNA Game Studio or Windows Phone SDK install failure on Windows 8
- Sicily 找中位数(Memory Limit:2MB)
- How to Increase the Memory Limit for 32-bit Applications in Windows 64-bit OS
- How to poke around the sqlite3 databases
- How tnsping work: Comparison of Oracle's tnsping to TCP/IP's ping [ID 146264.1]
- RTF field how to work
- How to work with Ant
- How to work hard easily!
- Title: How to limit instantialization?
- How To Enable Android's Developer Options
- How to get your Compaq 6715s wireless connection work on Ubuntu?
- cocos2d 2.x 实现精灵的会移动的遮罩
- zencart Notice: FAILED to send Email to all recipients. tool->send email
- ZOJ 1013 Great Equipment
- Java-改写equals方法必须遵循的准则
- QQ伤感日志_一点点揭开这撕心裂肺的伤
- How to work around Android’s 24 MB memory limit
- Latex 空格的设定
- Comet:基于 HTTP 长连接的“服务器推”技术
- srm 556 div1 500 LeftRightDigitsGame2(DP)
- gcc链接脚本
- cocos2d 2.x 实现精灵的会移动的遮罩
- 英文名大全及含义(男)
- MFC基于对话框的程序,按回车退出的解决办法
- Intent(action,data,type,schema,category,flag)