Monodroid - GC.Collect fails without any reason

 ̄綄美尐妖づ 提交于 2019-12-25 02:20:09

问题


I have an application with 4 tabs (TabActivity). By some reasons I call GC.Collect every time user switches tab (overriding OnPause Activity's method). Sometimes (approximately 1 time from 50-100 calls, but sometimes this happens when application just started) my application hangs in this moment.

Here is part of my code:

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;
  }
}

And here is corresponding Android log output

//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>

No any exception to been thrown, no any visible reason to fail. Application just hangs, after few seconds I get Android OS alert: "Oppps, your application got stuck. Force close or wait?"

Anybody faced with it?


回答1:


There was a bug in some old versions of monodroid: if you call GC.Collect and an http request in a parallel threads, this caused crash. Update to a latest version of monodroid.




回答2:


Having to do a GC.Collect() under any circumstance is a bad code smell. Figure out the underlying reason and perform corrective rearchitecture.



来源:https://stackoverflow.com/questions/7599737/monodroid-gc-collect-fails-without-any-reason

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!