ImageView 使用的内存永远不会释放
Memory used by ImageView is never released
在构建加载内容视图布局的对话框时 (setContentView)
我注意到一件奇怪的事:
加载的布局有一个带有此对话框背景的 ImageView:
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/preloader_bg_small"
android:id="@+id/background_img"
/>
每次对话框显示(在不同的活动中)它会耗尽内存 (30mb) 图像本身是从本地资源加载的 290k jpg 并且永远不会被释放
我已尝试以编程方式加载图像:
((ImageView)dialog.findViewById(R.id.background_img)).setImageResource(R.drawable.preloader_bg_small);
然后在关闭对话框之前卸载它
((ImageView)dialog.findViewById(R.id.background_img)).setImageDrawable(null);
但是只有在 activity 关闭之后内存才会被释放,而不是立即释放。
有没有办法释放内存?
为什么 ImageView 会这样?
感谢帮助!
Android 为您管理 Dialog
作为优化。不幸的是,它不会在使用后删除 Dialog
s。它让它们一直存在,希望您能再次使用它们。通常,这对您的应用程序来说不是优势。但这就是它的工作方式。
你需要做的是在你的Dialog
被解雇后移除(删除)它们。当您不再需要 Dialog
时,您可以通过调用 removeDialog()
来实现。
尝试((ImageView)dialog.findViewById(R.id.background_img)).setImageResource(android.R.color.transparent);
但它似乎不是图像视图问题。您的 activity 保持 link 图像。请给出更多代码。
尽管通过以下方式解决仍然很奇怪:
通过直接设置可绘制而不是资源来加载图像:
((ImageView)dialog.findViewById(R.id.background_img)).setImageDrawable(getResources().getDrawable(R.drawable.preloader_bg_small));
并通过将 drawable 设置为 null
将其从内存中释放
ImageView background_image = ((ImageView) dialog.findViewById(R.id.background_img));
background_image.setImageDrawable(null);
回收 ImageView
的 BitmapDrawable
资源的示例方法是使用以下函数:
protected void recycleDefaultImage() {
Drawable imageDrawable = imageView.getDrawable();
imageView.setImageDrawable(null); //this is necessary to prevent getting Canvas: can not draw recycled bitmap exception
if (imageDrawable!=null && imageDrawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageDrawable);
if (!bitmapDrawable.getBitmap().isRecycled()) {
bitmapDrawable.getBitmap().recycle();
}
}
}
在构建加载内容视图布局的对话框时 (setContentView) 我注意到一件奇怪的事: 加载的布局有一个带有此对话框背景的 ImageView:
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/preloader_bg_small"
android:id="@+id/background_img"
/>
每次对话框显示(在不同的活动中)它会耗尽内存 (30mb) 图像本身是从本地资源加载的 290k jpg 并且永远不会被释放
我已尝试以编程方式加载图像:
((ImageView)dialog.findViewById(R.id.background_img)).setImageResource(R.drawable.preloader_bg_small);
然后在关闭对话框之前卸载它
((ImageView)dialog.findViewById(R.id.background_img)).setImageDrawable(null);
但是只有在 activity 关闭之后内存才会被释放,而不是立即释放。
有没有办法释放内存? 为什么 ImageView 会这样?
感谢帮助!
Android 为您管理 Dialog
作为优化。不幸的是,它不会在使用后删除 Dialog
s。它让它们一直存在,希望您能再次使用它们。通常,这对您的应用程序来说不是优势。但这就是它的工作方式。
你需要做的是在你的Dialog
被解雇后移除(删除)它们。当您不再需要 Dialog
时,您可以通过调用 removeDialog()
来实现。
尝试((ImageView)dialog.findViewById(R.id.background_img)).setImageResource(android.R.color.transparent);
但它似乎不是图像视图问题。您的 activity 保持 link 图像。请给出更多代码。
尽管通过以下方式解决仍然很奇怪:
通过直接设置可绘制而不是资源来加载图像:
((ImageView)dialog.findViewById(R.id.background_img)).setImageDrawable(getResources().getDrawable(R.drawable.preloader_bg_small));
并通过将 drawable 设置为 null
将其从内存中释放ImageView background_image = ((ImageView) dialog.findViewById(R.id.background_img));
background_image.setImageDrawable(null);
回收 ImageView
的 BitmapDrawable
资源的示例方法是使用以下函数:
protected void recycleDefaultImage() {
Drawable imageDrawable = imageView.getDrawable();
imageView.setImageDrawable(null); //this is necessary to prevent getting Canvas: can not draw recycled bitmap exception
if (imageDrawable!=null && imageDrawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageDrawable);
if (!bitmapDrawable.getBitmap().isRecycled()) {
bitmapDrawable.getBitmap().recycle();
}
}
}