简体   繁体   English

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

[英]How is a bitmap's memory managed on Android?

I am following this article on Android developer, which quotes : 我正在关注Android开发者的这篇文章该文章引述:

On Android 2.3.3 (API level 10) and lower, the backing pixel data for a bitmap is stored in native memory. 在Android 2.3.3(API级别10)及更低版本中,位图的支持像素数据存储在本机内存中。 It is separate from the bitmap itself, which is stored in the Dalvik heap. 它与位图本身是分开的,位图本身存储在Dalvik堆中。 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. 从Android 3.0(API级别11)开始,像素数据与关联的位图一起存储在Dalvik堆上。

There is another article which confused me further : 还有一篇文章让我更加困惑:

A Bitmap is a thin wrapper around a native heap memory area that stores pixel data. Bitmap是存储像素数据的本机堆内存区域的薄包装器。

I have multiple doubts : 我有很多疑惑:

  1. What is the difference between Dalvik heap, native heap and native memory ? Dalvik堆,本机堆和本机内存有什么区别?
  2. How is a bitmap different from the pixel data ? 位图与像素数据有何不同? My understanding is that any image file (unless it's a vector image) is called bitmap - which is compressed. 我的理解是,任何图像文件(除非它是矢量图像)都称为位图 - 它是压缩的。 Android decompresses/decodes this information to render pixels on screen. Android解压缩/解码此信息以在屏幕上渲染像素。 If I am correct, why do we need the compressed bitmap anymore ? 如果我是对的,我们为什么还需要压缩位图?
  3. How does a class like BitmapRegionDecoder work ? BitmapRegionDecoder这样的类如何工作? My understanding is that the entire bitmap is decoded/decompressed first, and then areas out of bound are ignored - this will help in memory efficiency but will not make decoding faster. 我的理解是首先对整个位图进行解码/解压缩,然后忽略超出范围的区域 - 这将有助于提高内存效率,但不会使解码速度更快。 Am I correct ? 我对么 ?
  4. What exactly happens when a bitmap is recycled ? 当位图被回收时到底发生了什么?

When having an OutOfMemoryError, try the code below. 遇到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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM