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.

Ural 1119 Metro

题意:在m*n的地图上,有n个点有斜向的路径,已知这些斜向路径的位置,求从(0,0)到(m,n)的最短路径 链接:
  • luminous11
  • luminous11
  • 2015年05月14日 22:42
  • 278

E - 排序 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

  • lalalademaxi
  • lalalademaxi
  • 2016年07月10日 10:41
  • 896

Problem E: Matrix Problem (II) : Array Practice Time Limit: 1 Sec Memory Limit: 4 MB Submit: 8980

Problem E: Matrix Problem (II) : Array Practice Time Limit: 1 Sec  Memory Limit: 4 MB Submit: 8980 ...
  • Galaxy_12138
  • Galaxy_12138
  • 2017年12月15日 22:37
  • 106

杭电 HDU 1108 最小公倍数

最小公倍数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su...
  • lsgqjh
  • lsgqjh
  • 2015年03月28日 21:15
  • 760

关于github不给上传100m以上文件的错误:this exceeds GitHub's file size limit of 100.00 MB的解决方法

小伙伴们肯定试过上传不了超过100m以上的文件到github吧!所以我们需要用到一个叫lfs的工具专门用来上传大文件的!!! 先放一下我的github:
  • u014432647
  • u014432647
  • 2017年07月07日 12:06
  • 806

Github 修正上传时“this exceeds GitHub’s file size limit of 100 MB”错误

Github只允许上传最大100MB的文件,如果超过,则会被server reject则需:git filter-branch --force --index-filter "git rm --cac...
  • hellobinfeng
  • hellobinfeng
  • 2014年05月09日 04:15
  • 4888


II Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Submit Status ...
  • wjhuihuii
  • wjhuihuii
  • 2016年07月31日 17:02
  • 163

数的划分 搜索 ,DP

1006: 数的划分 Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lld Submitted: 44  Accepte...
  • wust_ZJX
  • wust_ZJX
  • 2017年04月06日 15:45
  • 235

数据量的问题是很多面试笔试中经常出现的问题  数据量的问题是很多面试笔试中经常出现的问题,比如 google...
  • sony315
  • sony315
  • 2011年02月23日 09:53
  • 430
您举报文章:How to work around Android’s 24 MB memory limit