當我有兩個簡單的活動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