简体   繁体   English

释放位图内存的正确方法

[英]Proper way to release bitmap memory

I'm developing my first Android app. 我正在开发我的第一个Android应用程序。 I have a method that creates two bitmaps and returns a third bitmap which is an overlay of the second bitmap on top of the first bitmap. 我有一个创建两个位图并返回第三个位图的方法,该图是第二个位图在第一个位图之上的覆盖图。 So basically, I don't need the two bitmaps once the third bitmap is created. 因此,基本上,一旦创建了第三个位图,我就不需要两个位图。 I've read some posts and articles about releasing bitmap memory and I'm a bit confused on how to handle it. 我已经阅读了一些有关释放位图内存的帖子和文章,但对如何处理它有点困惑。

Do I have to release the bitmaps myself? 我必须自己释放位图吗? If yes, what is the proper way to do so?Are they released when the method is finished? 如果是,这样做的正确方法是什么?方法完成后它们会被释放吗? Should I just let the garbage collector release it? 我应该让垃圾收集器释放它吗?

public static Bitmap bitmapResizeOverlay(Context context, Uri selectedImage, int maxWidth,
                                         int maxHeight, @DrawableRes int overlayImageResource) {
    Bitmap selectedBitmap = bitmapResize(context, selectedImage, maxWidth, maxHeight);
    Bitmap overlayBitmap = BitmapFactory.decodeResource(context.getResources(), overlayImageResource);
    return overlayBitmapToBottom (selectedBitmap, overlayBitmap);
}

  selectedBitmap.recycle() 

method is used always in when you want clear the memory occupied with bitmap. 当您要清除位图占用的内存时,始终使用此方法。 The down side of not recycling the bit map could be OOM (out of Memory Exception) 不回收位图的不利方面可能是OOM(内存不足)

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

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