Monodroid - GC.Collect 无故失败
我有一个带有 4 个选项卡的应用程序 (TabActivity)。由于某些原因,每次用户切换选项卡时我都会调用 GC.Collect (覆盖 OnPause Activity 的方法)。有时(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>
没有抛出任何异常,没有任何明显的失败原因。应用程序挂起,几秒钟后我收到 Android 操作系统警报:“Oppps,您的应用程序卡住了。强制关闭还是等待?”
有人面临过吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
某些旧版本的 monodroid 中存在一个错误:如果您在并行线程中调用 GC.Collect 和 http 请求,则会导致崩溃。更新到最新版本的 monodroid。
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.
在任何情况下都必须执行 GC.Collect() 是一种糟糕的代码味道。找出根本原因并执行纠正性重组。
Having to do a GC.Collect() under any circumstance is a bad code smell. Figure out the underlying reason and perform corrective rearchitecture.