如何释放在Android的位图内存 [英] how to release bitmap memory in android
问题描述
在我们的程序を用大量的图片,我们unbindDrawables在eyery活动和片段如下
In our program wo use lots of images,we unbindDrawables in eyery Activity and Fragment as below
protected void unbindDrawables(View view) {
if (view != null) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ImageView) {
ImageView imageView = (ImageView) view;
imageView.setImageDrawable(null);
}
if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
if (!(view instanceof AbsSpinner) && !(view instanceof AbsListView)) {
((ViewGroup) view).removeAllViews();
}
}
}
}
这样的 http://stackoverflow.com/a/6779067 说,但我用的垫子发现,位图也使用大量内存和不释放。
like this http://stackoverflow.com/a/6779067 said,but I use the mat find that the bitmap also use a lots of memory and not release.
Class Name | Objects | Shallow Heap | Retained Heap
android.graphics.Bitmap | 490 | 23,520 | >= 36,267,912
android.graphics.Bitmap$1 | 1 | 8 | >= 8
android.graphics.Bitmap$2 | 0 | 0 | >= 80
android.graphics.Bitmap$BitmapFinalizer | 490 | 7,840 | >= 7,840
android.graphics.Bitmap$CompressFormat | 3 | 72 | >= 232
android.graphics.Bitmap$CompressFormat[]| 1 | 24 | >= 24
android.graphics.Bitmap$Config | 4 | 96 | >= 360
android.graphics.Bitmap$Config[] | 2 | 72 | >= 72
android.graphics.BitmapFactory | 0 | 0 | >= 80
android.graphics.BitmapFactory$Options | 0 | 0 |
android.graphics.BitmapRegionDecoder | 0 | 0 | >= 48
android.graphics.BitmapShader | 9 | 216 | >= 15,736
Total: 12 entries (4,509 filtered) | 1,000 | 31,848 |
我不知道为什么它没有被释放,如何释放它,任何机构可以帮助我,非常感谢!
I don't know why it not be released and how to release it,any body can help me,thanks a lot!
推荐答案
您必须调用bitmap.recycle()来释放分配的本地内存,当你去codeD位图。
You must call bitmap.recycle() to release the native memory allocated when you decoded the bitmap.
当然,你必须把位图的生命周期的服务。要在适当的时间将其释放。更好的方法是使用一些图像加载器类来照顾它。请参见 https://github.com/nostra13/Android-Universal-Image-Loader
And of course you must have to take care of the life cycle of the bitmap. To release it at proper time. A better solution is to use some image loader class to take care of it. See https://github.com/nostra13/Android-Universal-Image-Loader
这篇关于如何释放在Android的位图内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!