我一直在制作一个模仿ViewFlipper的菜单系统,除非我自己重置RootView中的图像和文本,试图避免与Bitmap相关的OOM。一切都很顺利,直到昨天,当我意识到经过几次重新启动后,随后的视图更改,我会得到位图超出VM预算..或类似的东西。我开始分配跟踪,试图看到什么没有被抛弃,并发现这些电话我的麻烦来源:位图内存泄漏?
_stars.setBackgroundDrawable(getResources().getDrawable(R.drawable.star_1));
_button.setBackgroundResource(R.drawable.button_1);
_image.setImageResource(R.drawable.image_1);
这显然不是所有的代码,但它是在跟踪指向我了MANY分配是这样的:
258 72 android.graphics.BitmapFactory$Options 1 android.graphics.drawable.Drawable
481 68 android.graphics.drawable.BitmapDrawable 1 android.graphics.drawable.Drawable
482 52 android.graphics.Paint 1 android.graphics.Bitmap createBitmap
479 52 android.graphics.Paint 1 android.graphics.drawable.BitmapDrawable$BitmapState
255 36 android.graphics.Bitmap 1 android.graphics.BitmapFactory
254 36 android.graphics.Canvas 1 android.graphics.Bitmap createBitmap
250 36 android.graphics.Bitmap 1 android.graphics.Bitmap nativeCreate
123 36 android.graphics.Bitmap 1 android.graphics.BitmapFactory
什么我不知道,这正常吗?或者我有泄漏的地方?测试结束后,我的配置中至少有10-20这些在我的分配中,最终我不可避免地击中了OOM。不知道如何照顾这一点,而不是使用Bitmap变量,然后使用Bitmap.recycle(),但涉及检查视图是否仍在使用,并可能在错误的时间回收;因此我不是粉丝。每次我退出视图时,我更多的是寻找一种方法来终止所有这些分配。 (已经尝试设置控件为空的onPause()&的onDestroy(),希望他们会释放引用的位图,从而能够将它们GC无济于事)
[编辑]
我已阅读here当您的活动完成后,Drawables将根据需要进行处理。所以我应该不需要给他们打电话回收呢?至于单个Bitmap的大小,它只是一个图像(480w x 720h(x4bpp/8))/ 1024 =〜169KB,这似乎不是问题。
我已经添加了一些编辑,可以解决您所说的问题,而无需在此处进行操作。 –