我有一個包含4個選項卡(TabActivity)的應用程序。由於某些原因,我打電話GC.Collect每次用戶切換選項卡(重寫OnPause活動的方法)。有時(從50-100次調用中大約需要1次,但有時會在應用程序剛剛啓動時發生),我的應用程序在此刻掛起。Monodroid - GC.Collect無故失敗
這裏是我的代碼部分:
protected override void OnPause(){
base.OnPause();
try{
Android.Util.Log.Info("----","GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);");
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
Android.Util.Log.Info("----","GC.Collect Finished");
}catch(Exception exc){
Android.Util.Log.Info("exc.Message",exc.Message);
Android.Util.Log.Info("exc.StackTrace",exc.StackTrace);
throw exc;
}
}
,這裏是相應的Android日誌輸出
//Previous GC.Collect call, it's all ok
I/---- (7796): GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
D/dalvikvm(7796): GetMethodID: method not found: Landroid/widget/EditText;.monodroidAddReference:(Ljava/lang/Object;)V
D/dalvikvm(7796): GC_EXPLICIT freed 962 objects/42472 bytes in 112ms
I/---- (7796): GC.Collect Finished
//On another call fails
I/---- (7796): GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
I/mono (7796): Stacktrace:
I/mono (7796):
I/mono (7796): at System.GC.Collect (int) <0x0001f>
I/mono (7796): at System.GC.Collect (int,System.GCCollectionMode) <0x00017>
I/mono (7796): at PixelsAndroid.CustomActivity.OnPause() <0x00067>
I/mono (7796): at Android.App.Activity.n_OnPause (intptr,intptr) <0x00037>
I/mono (7796): at (wrapper dynamic-method) object.908cefd4-40eb-4dd1-97cd-f731b2ada74a (intptr,intptr) <0x0002b>
I/mono (7796): at (wrapper native-to-managed) object.908cefd4-40eb-4dd1-97cd-f731b2ada74a (intptr,intptr) <0xffffffff>
沒有任何例外,被扔,沒有任何明顯理由失敗。應用程序只是掛起,幾秒鐘後,我得到Android操作系統警報:「Oppps,你的應用程序卡住了,強迫關閉還是等待?
有人遇到它嗎?
你爲什麼在第一時間調用GC.Collect()?我從來沒有一個合理的理由來強制在MonoDroid或Windows上進行GC處理。當我看到GC.Collect()時,它是一種需要重新思考的代碼味道。 –
我使用它就像[這個問題]的特定解決方案(http://stackoverflow.com/questions/7603135/monodroid-passing-data-to-listview-according-to-gref-limit) – PVoLan
減少實例的數量你加載。坦率地說,如果你的代碼加載了太多的列表項目以至於你達到了GREF限制(即你沒有分頁數據),那麼你需要重新設計你的解決方案。請記住,這是一個移動設備,而不是你的筆記本電腦/臺式機/任何。 –