Android Monodroid-GC.Collect无任何原因失败

Android Monodroid-GC.Collect无任何原因失败,android,mono,xamarin.android,Android,Mono,Xamarin.android,我有一个带有4个选项卡的应用程序(TabActivity)。由于某些原因,每次用户切换选项卡时,我都调用GC.Collect(覆盖OnPause活动的方法)。有时(大约50-100次调用中的1次,但有时在应用程序刚启动时发生)我的应用程序挂起 以下是我的部分代码: protected override void OnPause(){ base.OnPause(); try{ Android.Util.Log.Info("----","GC.Collect(GC.MaxGener

我有一个带有4个选项卡的应用程序(TabActivity)。由于某些原因,每次用户切换选项卡时,我都调用GC.Collect(覆盖OnPause活动的方法)。有时(大约50-100次调用中的1次,但有时在应用程序刚启动时发生)我的应用程序挂起

以下是我的部分代码:

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>
//上次GC.对方付费电话,一切正常
I/---(7796):GC.Collect(GC.MaxGeneration,GCCollectionMode.Forced);
D/dalvikvm(7796):GetMethodID:找不到方法:Landroid/widget/EditText;。monodroid地址引用:(Ljava/lang/Object;)V
D/dalvikvm(7796):GC_在112ms内显式释放962个对象/42472字节
I/---(7796):GC.收集完成
//另一个呼叫失败
I/---(7796):GC.Collect(GC.MaxGeneration,GCCollectionMode.Forced);
I/mono(7796):堆栈跟踪:
I/mono(7796):
I/mono(7796):位于System.GC.Collect(int)
I/mono(7796):位于System.GC.Collect(int,System.GCCollectionMode)
I/mono(7796):在PixelsAndroid.CustomActivity.OnPause()处
I/mono(7796):位于Android.App.Activity.n_OnPause(intptr,intptr)
I/mono(7796):at(包装器动态方法)object.908cefd4-40eb-4dd1-97cd-f731b2ada74a(intptr,intptr)
I/mono(7796):at(托管的本机包装器)object.908cefd4-40eb-4dd1-97cd-f731b2ada74a(intptr,intptr)
没有任何异常被抛出,没有任何可见的失败原因。应用程序刚刚挂起,几秒钟后我收到Android操作系统警报:“Oppps,你的应用程序卡住了。强制关闭还是等待?”


有人遇到过这个问题吗?

在一些旧版本的monodroid中有一个bug:如果调用GC.Collect并在并行线程中执行http请求,就会导致崩溃。更新到monodroid的最新版本。

在任何情况下都必须执行GC.Collect(),这是一种糟糕的代码味道。找出根本原因并执行纠正性的重新体系结构。

首先为什么要调用GC.Collect()?我从来没有一个有效的理由来强制MonoDroid或Windows上的GC。当我看到GC.Collect()时,它是一种需要重新思考的代码味道。我将它用作减少加载实例数量的特定解决方案。坦率地说:如果您的代码加载了太多的列表项,以至于您达到了GREF限制(即,您没有分页数据),那么您需要重新设计您的解决方案。记住,这是一个移动设备,而不是你的笔记本电脑/台式机/任何东西。