最初从位图泄漏了未引用的byte [],但被回收的()导致内存泄漏(直到活动停止) [英] Leaked unreferenced byte[] originally from bitmap but recycled() causing memory-leak (until activity stopped)

查看:95
本文介绍了最初从位图泄漏了未引用的byte [],但被回收的()导致内存泄漏(直到活动停止)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的位图内存泄漏导致内存不足.我在Android 5.0(Samsung S5)上运行了测试.我已经使用Android Studio(1.5.1 + 2.0.0预览版7)调查了此问题. HPROF内存转储显示有多个byte []完全对应于我临时使用的特定巨大位图.如果我确定保留对位图的引用,则Android Studio将向我显示一个11MB大小的位图和一个11MB浅尺寸的byte [].如果我不保留对位图的引用,则某些位图将被垃圾回收,并且某些最终会以byte []的形式出现,而没有传入的引用(即,没有父级),如图所示.

I have a memory leak of bitmaps causing out of memory. I ran the tests on Android 5.0 (Samsung S5). I have investigated the issue using Android Studio (1.5.1 + 2.0.0 Preview 7). The HPROF memory dump show that there are multiple byte[] which correspond exactly to a particular huge bitmap I use temporarily. If I make sure I keep references to the bitmap then Android Studio shows me a Bitmap with 11MB dominating size and a byte[] with 11MB Shallow size. If I don't keep references to the bitmaps then some of the bitmaps become garbage collected and some end up as byte[] without incoming references (ie. no parents) as shown in image.

我已经对我的应用程序进行了充分的测试,足以有把握地知道这个11MB字节[]是我在内存中存储的大约2891x1000x4位图.一些较小的位图也会泄漏,并且没有传入的引用出现.

I have tested my app enough to know with reasonable confidence that this 11MB byte[] is a roughly 2891x1000x4 bitmap I have had in memory. Some smaller bitmaps also get leaked and show up with no incoming references.

上面的位图在子活动中分配.如果我返回到父活动(在相同的进程中并因此在dalvikVM中)并强制执行2x GC,则释放内存.退出子活动之前,多个手动GC不会释放内存.

The bitmaps above are allocated in a subactivity. If I return to the parent activity (in same process and thus dalvikVM) and force 2x GC then memory is released. Multiple manual GC's does not release the memory before quitting the sub-activity.

这似乎与我是否运行bitmap.recycle()无关.如果我只是站在应用程序中的同一位置并运行从同一视图中生成巨大位图的代码,则这种情况很少发生.如果我四处移动应用程序并从不同的视图生成位图,则位图的出现频率会更高,例如从50%泄漏到10%泄漏.

It seems to be independent on whether I run bitmap.recycle() or not. It happens very rarely if I just stand in the same place in the app and run the code generating the huge bitmap out of the same view. If I move around the app and generate the bitmap from different views its a lot more frequent, like from 50% leaks to 10% leaks.

  • 这是Android的错误吗?
  • 我是否以某种方式使用了错误的位图,而Android Studio只是无法向我显示正确的内存视图?
  • 我的理解是正确的,如果下面的参考树中没有父母(见图),那么内存应该由垃圾收集器释放(因此它是Android Bug或Studio Bug还是hprof dump-bug?)

推荐答案

尽管Android Studios报告了未引用的字节[]仍然是个谜,但我找到了解决泄漏的方法.

I found a solution to the leak, though Android Studios reporting of the unreferenced byte[] is still a mystery.

看来我做过的ImageView

It appears that the ImageView in which i did

imageView.setImageBitmap(bitmap)

将防止对基于byte []的位图进行GC. bitmap.recycle()无济于事,unbinddrawables()也无济于事

will prevent GC of the bitmaps underlying byte[]. bitmap.recycle() does not help, nor does unbinddrawables() as written about in numerous locations

if (imageView.getBackground() != null) {
    imageView.getBackground().setCallback(null);
}
setImageBackground(imageView, null);
imageView.setImageBitmap(null);
imageView.setImageDrawable(null);

当我从视图层次结构中删除视图并删除我自己对该视图的所有引用时,然后将byte []进行了GC处理,泄漏就消失了.

When I removed the view from the viewhiearchy and removed all my own references to the view, then the byte[] was GC'ed and the leak was gone.

这篇关于最初从位图泄漏了未引用的byte [],但被回收的()导致内存泄漏(直到活动停止)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆