当前位置:操作系统 > 安卓/Android >>

android 管理Bitmap内存 - 开发文档翻译

Managing Bitmap Memory
管理Bitmap内存
 
In addition to the steps described in Caching Bitmaps, there are specific things you can do to facilitate garbage collection and bitmap reuse. 
The recommended strategy depends on which version(s) of Android you are targeting. 
The BitmapFun sample app included with this class shows you how to design your app to work efficiently across different versions of Android.
除了在Caching Bitmaps里描述的,还有一些具体的事情有助于垃圾回收和bitmap的重用
建议的策略取决于你针对的Android版本。
BitmapFun样本应用中包括这个类,它展示给你如何设计你的应用使得跨版本工作更有效率
 
To set the stage for this lesson, here is how Android's management of bitmap memory has evolved:
为课程打基础,下面是android的bitmap内存管理是如何演化的
 
On Android Android 2.2 (API level 8) and lower, when garbage collection occurs, your app's threads get stopped. 
This causes a lag that can degrade performance. 
Android 2.3 adds concurrent garbage collection, which means that the memory is reclaimed soon after a bitmap is no longer referenced.
在Android2.2(API 8)以及更低的版本中,当发生垃圾回收时,你的应用线程会停止。
这会导致延迟,使得性能降低
Android2.3添加了并发垃圾收集,这意为着一个bitmap不再被引用的时候,内存很快就被回收
 
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.
在Android2.3.3(API 10)和更低的版本中,bitmap的像素数据是存储在native内存中的
它独立于bitmap本身,bitmap是存储在Dalvik堆中的
在native内存中的bitmap的像素数据不会在一个可预测的行为之释放,潜在的导致应用内存超过限制并且崩溃
在android3.0(API 11)中,bitmap的像素数据存储在Dalvik堆中于bitmap相关联
 
The following sections describe how to optimize bitmap memory management for different Android versions.
下面章节讲述在不同的android版本中,如何最优化bitmap内存管理
 
 
Manage Memory on Android 2.3.3 and Lower
在android2.3.3以及更低的版本中管理内存
 
On Android 2.3.3 (API level 10) and lower, using recycle() is recommended. 
If you're displaying large amounts of bitmap data in your app, you're likely to run into OutOfMemoryError errors. 
The recycle() method allows an app to reclaim memory as soon as possible.
在android2.3.3以及更低的版本中,建议使用recycle()
如果你在你的应用中大量的显示bitmap数据,你很可能得到OutOfMemoryError错误
recycle()方法允许一个应用尽快回收内存
 
Caution: You should use recycle() only when you are sure that the bitmap is no longer being used. 
If you call recycle() and later attempt to draw the bitmap, you will get the error: "Canvas: trying to use a recycled bitmap".
注意:仅当你确定这个bitmap不会再被使用的时候,你才应该使用recycle()
如果你调用了recycle(),之后又试图绘制这个bitmap,你会得到 错误:“Canvas: trying to use a recycled bitmap”
 
The following code snippet gives an example of calling recycle(). 
It uses reference counting (in the variables mDisplayRefCount and mCacheRefCount) to track whether a bitmap is currently being displayed or in the cache. 
The code recycles the bitmap when these conditions are met:
下面的代码片断给出了一个调用recycle()例子
它使用引用计数(在变量mDisplayRefCount 和 mCacheRefCount中)来根中一个bitmap当前正在被显示还是在缓存中
代码回收bitmap需要的条件是:
 
The reference count for both mDisplayRefCount and mCacheRefCount is 0.
The bitmap is not null, and it hasn't been recycled yet.
引用计数mDisplayRefCount和mCacheRefCount都要=0
bitmap不为null,并且它还没有被回收
[java]  
private int mCacheRefCount = 0;  
private int mDisplayRefCount = 0;  
...  
// Notify the drawable that the displayed state has changed.  
// Keep a count to determine when the drawable is no longer displayed.  
public void setIsDisplayed(boolean isDisplayed) {  
    synchronized (this) {  
        if (isDisplayed) {  
            mDisplayRefCount++;  
            mHasBeenDisplayed = true;  
        } else {  
            mDisplayRefCount--;  
        }  
    }  
    // Check to see if recycle() can be called.  
    checkState();  
}  
   
// Notify the drawable that the cache state has changed.  
// Keep a count to determine when the drawable is no longer being cached.  
public void setIsCached(boolean isCached) {  
    synchronized (this) {  
        if (isCached) {  
            mCacheRefCount++;  
        } else {  
            mCacheRefCount--;  
        }  
    }  
    // Check to see if recycle() can be called.  
    checkState();  
}  
   
private synchronized void checkState() {  
    // If the drawable cache and display ref counts = 0, and this drawable  
    // has been displayed, then recycle.  
    if (mCacheRefCount <= 0 && mDisplayRefCount <= 0 && mHasBeenDisplayed  
            && hasValidBitmap()) {  
        getBitmap().recycle();  
    }  
}  
   
private synchronized boolean hasValidBitmap() {  
    Bitmap bitmap = getBitmap();  
    return bitmap != null && !bitmap.isRecycled();  
}  
Manage Memory on Android 3.0 and Higher
在android3.0以及更高的版本中管理内存
 
Android 3.0 (API Level 11) introduces the BitmapFactory.Options.inBitmap field. 
If this option is set, decode methods that take the Options object will attempt to reuse an existing bitmap when loading content. 
This means that the bitmap's memory is reused, resulting in improved performance, and removing both memory allocation and de-allocation. 
There are some caveats in using inBitmap:
Android3.0(API 11)引入了BitmapFactory.Options.inBitmap
如果设置了这个选项,当加载内容的时候,使用Options对象的解码方法将会尝试复用一个存在的bitmap
这意为着bitmap的内存
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,