当我有两个简单的活动A
和B
。通过按下按钮,用户从A
开始B
,然后用户通过按下后退按钮返回到A
。循环的竞争位图 - “试图使用回收的位图”重新推出活动
在onDestroy()
活动方法B
中,我回收了活动B
中使用的一些背景图像。我试图理解的是为什么当活动B
再次启动时,我正在'尝试使用循环位图'。当然,位图会在onCreate()方法中再次加载?就像他们第一次开展活动一样。
这里是我的示例代码:
public class ActivityB extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.selectionpage);
}
@Override
public void onDestroy() {
ImageView iv = (ImageView) findViewById(R.id.imageView1);
((BitmapDrawable)iv.getDrawable()).getBitmap().recycle();
LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout1);
((BitmapDrawable)ll.getBackground()).getBitmap().recycle();
super.onDestroy();
}
}
我用从发射活动B
代码A
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
selectionpage.XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backgroundimage">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/selectionimage"/>
</LinearLayout>
这部分可能是相关的。我不确定。我注意到,在启动活动B之后,即使它已被销毁,我仍然可以在使用MAT分析内存堆时看到我的活动实例。 GC根的路径似乎要经过Java.lang.Thread和ContextImpl。
是啊。我是一个白痴 - 我没有意识到这就是xml文件的工作原理。尽管我注意到的一件事是,在我的程序的另一个区域,我似乎在加载多个大图像后出现内存不足错误。即使一些图像不再被使用,GC似乎也不想删除它们。我不得不打电话给recycle() - 我相信要通知GC立即清理它们,而不是等一会儿。无论如何,我在散散步。感谢您的回答 – Wozza
对于所有版本的android,这并非如此:'在Android 2.3.3(API级别10)及更低版本中,位图的后备像素数据存储在本机内存中。它与存储在Dalvik堆中的位图本身是分开的。原生内存中的像素数据不会以可预测的方式释放,可能会导致应用程序短暂超出内存限制并导致崩溃[http://developer.android.com/training/displaying-bitmaps/manage-memory]。html#inBitmap – ForceMagic