首页 > 解决方案 > 如何从 ADB shell 强制/触发 xamarin mono 到 GC?

问题描述

尝试使用此命令adb shell debug.mono.profile获取堆射击。

那么使用 adb shell 触发 GC 的命令是什么?

标签: androidxamarinxamarin.androidmonoadb

解决方案


添加到调用覆盖的BroadcastReceiver应用程序。GC.CollectOnReceive

这是我使用的一个:

#if DEBUG
[BroadcastReceiver(Name = ReceiverName, Enabled = true, Exported = true)]
[IntentFilter(new string[] { IntentFilterName })]
public class MonoGCBroadcastReceiver : BroadcastReceiver
{
    const string ReceiverName = "com.sushihangover.someapp.garbagecollect";
    const string IntentFilterName = "perform.mono.gc";
    readonly string TAG = Application.Context.PackageName;

    public override void OnReceive(Context context, Intent intent)
    {
        if (intent.Action == IntentFilterName)
        {
            var generation = intent.GetIntExtra("generation", 9999);
            if (!Enum.TryParse(intent.GetStringExtra("mode"), true, out GCCollectionMode mode))
            {
                mode = GCCollectionMode.Forced;
            }
            var force = intent.GetBooleanExtra("forced", true);
            var compacting = intent.GetBooleanExtra("compacting", true);
            Log.Debug(TAG, $"GC: G:{generation} : M:{mode} : F:{force} C:{compacting}");
            GC.Collect(generation, mode, force, compacting);
        }
    }
}
#endif

现在,您可以使用adb调用该接收器并为垃圾收集器传递可选参数。

例子:

export packageName="com.sushihangover.someapp"

adb shell setprop debug.mono.profile log:heapshot

# Start you app

adb shell am broadcast \
  -n $packageName/.garbagecollect \
  -a perform.mono.gc \
  --es mode "force" \
  --ei generation 1 \
  --ez forced true

// Clear debug.mono.profile when all done collecting your mlpd
adb shell setprop debug.mono.profile '""'

推荐阅读