阅读更多 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.

从上面来看使用navtive code分配内存是不在24MB的限制内的(open GL的texture也是使用navtive code分配内存的)。

更多相关文章

  1. 箭头函数的基础使用
  2. NPM 和webpack 的基础使用
  3. Python list sort方法的具体使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. 【Android】loadData与loadDataWithBaseURL的区别
  6. Android(安卓)使用HttpGet进行登录
  7. OpenGL ES教程VI之纹理贴图(原文对照)
  8. Android(安卓)Wifi的使用
  9. Drawable简单使用

随机推荐

  1. Android跨进程通信之非AIDL(二)
  2. 视图模式:Android ViewPager & WP Pivot
  3. Android问题(一)Android Studio 提示"Error
  4. Android(安卓)SDK更新后出现No target av
  5. Android生成和扫描二维码之ZXing
  6. 【Android】自定义控件之View原理与使用
  7. Android面试简录——组件
  8. Android系统架构概况
  9. repost:Android shell 下 busybox,clear,tc
  10. 收集的android开源项目,android学习必备