android - 什么时候(如果有的话)我应该使用 Bitmap.recycle()?

标签 android bitmap garbage-collection out-of-memory android-imageview

根据 Android Developers 网站,Bitmap.recycle() 方法定义为:

Free the native object associated with this bitmap, and clear the reference to the pixel data

我开发了一些创建/解码大量位图的应用程序,并将结果位图对象放入 ImageView 中。有时我会遇到著名的异常,例如:

bitmap size excceded vm budget

out of memory error

此外,我确定我没有任何可能导致这种情况的内存泄漏。

经过大量搜索,我发现了“回收”方法,并在不再需要时使用它来释放位图的 native 内存。 看起来很有帮助

我想问在这种情况下我是否应该这样做,因为 我知道系统无论如何都在执行此操作而没有显式调用它(是吗?也许我错了)。

我应该在这种情况下使用这种方法吗?

什么情况下应该使用这个方法?

我是否应该使用这种方法?

提前致谢。

更新:

谷歌发布this最近的指南,上面写着:

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.

最佳答案

in what situations should I use this method?

只要 GC 决定,Bitmap 就会被 GC。但在某些情况下,它可能会延迟。 并且永远记住java中的经验法则(也许它也适用于其他P.L)。GC回收对象的速度可能与创建对象的速度不同。所以有时GC回收速度很慢。

所以 recycle() 意味着如果你想尽快释放内存,你应该调用 recycle()

should I use this method at all??

这是一个高级调用,通常不需要调用,因为正常的 GC 过程会在不再引用此位图时释放此内存。但是如果您遇到超出位图大小的问题vm budgetout of memory error 那么你需要使用它。

关于android - 什么时候(如果有的话)我应该使用 Bitmap.recycle()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11202313/

相关文章:

android - ViewBinding - 如何获得包含布局的绑定(bind)?

java - "Class Cast Exception"尝试将图像帧设置为背景时

Android:BitmapFactory 解码速度极慢

android - 如何在 Android 中为位图着色?

c# - 事件触发前的对象处理和垃圾收集

javascript - 网页 View 不加载网页图像

Android字节数组到位图如何

c# - 为什么非托管代码会将内存增加到特定限制?

memory-management - 覆盖内存中的数据

java - 在 android studio (1.1.0) 中,如何添加对来自其他来源的 java 文件的依赖?