java - 如何在 Android 上管理位图的内存?

标签 java android memory bitmap garbage-collection

我正在关注 this article在 Android 开发者上,引用:

On Android 2.3.3 (API level 10) and lower, the backing pixel data for a bitmap is stored in native memory. It is separate from the bitmap itself, which is stored in the Dalvik heap. The pixel data in native memory is not released in a predictable manner, potentially causing an application to briefly exceed its memory limits and crash. As of Android 3.0 (API level 11), the pixel data is stored on the Dalvik heap along with the associated bitmap.

another article这让我更加困惑:

A Bitmap is a thin wrapper around a native heap memory area that stores pixel data.

我有很多疑问:

  1. Dalvik 堆、 native 堆和 native 内存之间有什么区别?
  2. 位图与像素数据有何不同?我的理解是任何图像文件(除非它是 vector 图像)都称为位图 - 它是压缩的。 Android 解压缩/解码此信息以在屏幕上呈现像素。如果我是对的,为什么我们还需要压缩位图?
  3. 类如何BitmapRegionDecoder工作 ?我的理解是整个位图首先被解码/解压缩,然后忽略边界外的区域——这将有助于提高内存效率,但不会使解码速度更快。我说得对吗?
  4. 回收位图时究竟发生了什么?

最佳答案

遇到 OutOfMemoryError 时,请尝试以下代码。

try {
    bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
    return bitmap;
} catch (OutOfMemoryError e) {
    bitmap.recycle();
    bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
    return bitmap;
}

关于java - 如何在 Android 上管理位图的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33447955/

相关文章:

ios - Monotouch-启用Guard Malloc

java - 添加新数据时 GraphView 不更新

mac 上大文件的 java.io.RandomAccessFile 无效参数

java - 缓存和持久化数据

Android JUnit 测试未在 Robotium 中检测到

android - 当 Edittext 获得焦点时,软键盘按下底部操作栏

java - 关于使用 stub - Java

android - adb 不会检测到,但无法识别我的设备

c++ - 如果通过委托(delegate)给 `free` 的重载 `new[]` 分配内存,那么 `malloc` 内存是否安全?

memory - 内存通常比磁盘快多少?