由于使用 ParallelTask​​ 和 ConcurrentStack 的单声道导致 Android 崩溃

Crash on Android arising from mono for using ParallelTask and ConcurrentStack usage

描述

我从单声道中遇到崩溃,从徽标中我没有得到任何我可以在代码中更改的具体内容

[] * Assertion at /Users/builder/jenkins/workspace/archive-mono/2019-06/android/release/mono/metadata/sgen-tarjan-bridge.c:1140, condition `xref_count == xref_index' not met, function:processing_build_callback_data, xref_count is 47 but we added 45 xrefs
[libc] Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 26216 (Thread Pool Wor), pid 25949 (ileapp_colorpoc)

不确定根本原因是什么,因为相同的代码可以正常运行 2 次迭代。我将所有内容都包装在 try catch 中,看看是否有我可以捕获的异常。垃圾收集器似乎有问题

更多日志

11-19 09:28:26.586 D/ViewRootImpl@5d456e4[MainActivity]( 2812): ViewPostIme pointer 0
11-19 09:28:26.710 D/ViewRootImpl@5d456e4[MainActivity]( 2812): ViewPostIme pointer 1
Thread finished: <Thread Pool> #50
The thread 0x32 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #8
The thread 0x8 has exited with code 0 (0x0).
11-19 09:28:45.387 D/ViewRootImpl@5d456e4[MainActivity]( 2812): ViewPostIme pointer 0
11-19 09:28:45.506 D/ViewRootImpl@5d456e4[MainActivity]( 2812): ViewPostIme pointer 1
11-19 09:28:46.147 D/Mono    ( 2812): GC_BRIDGE waiting for bridge processing to finish
11-19 09:28:46.197 I/zygote64( 2812): Explicit concurrent copying GC freed 914(91KB) AllocSpace objects, 0(0B) LOS objects, 60% free, 3MB/9MB, paused 188us total 25.829ms
11-19 09:28:46.197 D/Mono    ( 2812): GC_TAR_BRIDGE bridges 70 objects 70 opaque 0 ABCs 70 ABCs-bridged 70 ABCs-visible 70 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.11ms tarjan 0.02ms scc-setup 0.02ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.05ms
11-19 09:28:46.197 D/Mono    ( 2812): GC_BRIDGE: Complete, was running for 26.93ms
11-19 09:28:46.197 D/Mono    ( 2812): GC_MINOR: (Nursery full) time 4.43ms, stw 36.62ms promoted 312K major size: 22960K in use: 21279K los size: 33520K in use: 30683K
Thread started: <Thread Pool> #66
Thread started: <Thread Pool> #67
Thread started: <Thread Pool> #68
Thread started: <Thread Pool> #69
Thread started: <Thread Pool> #70
Thread started: <Thread Pool> #71
Thread started: <Thread Pool> #72
Thread started: <Thread Pool> #73
Thread started: <Thread Pool> #74
Thread started: <Thread Pool> #75
11-19 09:28:47.362 D/Mono    ( 2812): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 ABCs 0 ABCs-bridged 0 ABCs-visible 70 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.11ms tarjan 0.02ms scc-setup 0.02ms gather-xref 0.00ms xref-setup 0.00ms cleanup 0.00ms
11-19 09:28:47.362 D/Mono    ( 2812): GC_BRIDGE: Complete, was running for 0.12ms
11-19 09:28:47.362 D/Mono    ( 2812): GC_MINOR: (Nursery full) time 6.18ms, stw 8.96ms promoted 1072K major size: 24048K in use: 22372K los size: 33520K in use: 30898K
11-19 09:28:47.420 I/Choreographer( 2812): Skipped 114 frames!  The application may be doing too much work on its main thread.
11-19 09:28:47.439 D/InputMethodManager( 2812): HSIFW - flag : 0 Pid : 2812
[0:] no non-static method "Landroid/support/v7/widget/AppCompatButton;.setOutlineAmbientShadowABC(I)V": Cannot set property on attached control. Error: 
11-19 09:28:47.977 I/zygote64( 2812): Explicit concurrent copying GC freed 1730(279KB) AllocSpace objects, 0(0B) LOS objects, 59% free, 4MB/10MB, paused 192us total 24.513ms
11-19 09:28:47.982 D/Mono    ( 2812): GC_TAR_BRIDGE bridges 54 objects 124683 opaque 79915 ABCs 54 ABCs-bridged 54 ABCs-visible 54 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.06ms tarjan 37.60ms scc-setup 0.05ms gather-xref 0.00ms xref-setup 0.00ms cleanup 3.79ms
11-19 09:28:47.982 D/Mono    ( 2812): GC_BRIDGE: Complete, was running for 29.27ms
11-19 09:28:47.982 D/Mono    ( 2812): GC_MINOR: (Nursery full) time 50.29ms, stw 53.89ms promoted 1007K major size: 25104K in use: 23388K los size: 33520K in use: 31980K
11-19 09:28:49.369 D/ViewRootImpl@5d456e4[MainActivity]( 2812): ViewPostIme pointer 0
11-19 09:28:49.436 D/ViewRootImpl@5d456e4[MainActivity]( 2812): ViewPostIme pointer 1
11-19 09:28:49.473 I/zygote64( 2812): Do full code cache collection, code=1006KB, data=654KB
11-19 09:28:49.478 I/zygote64( 2812): After code cache collection, code=993KB, data=595KB
11-19 09:28:50.120 D/ViewRootImpl@5d456e4[MainActivity]( 2812): ViewPostIme pointer 0
11-19 09:28:50.229 D/ViewRootImpl@5d456e4[MainActivity]( 2812): ViewPostIme pointer 1
Thread finished: <Thread Pool> #53
The thread 0x35 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #24
The thread 0x18 has exited with code 0 (0x0).
11-19 09:29:00.327 D/ViewRootImpl@5d456e4[MainActivity]( 2812): ViewPostIme pointer 0
11-19 09:29:00.421 D/ViewRootImpl@5d456e4[MainActivity]( 2812): ViewPostIme pointer 1
11-19 09:29:00.536 D/Mono    ( 2812): GC_BRIDGE waiting for bridge processing to finish
11-19 09:29:00.562 I/zygote64( 2812): Explicit concurrent copying GC freed 921(62KB) AllocSpace objects, 0(0B) LOS objects, 58% free, 4MB/10MB, paused 198us total 25.136ms
11-19 09:29:00.569 D/Mono    ( 2812): GC_TAR_BRIDGE bridges 172 objects 128603 opaque 81203 ABCs 172 ABCs-bridged 172 ABCs-visible 172 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.04ms tarjan 36.83ms scc-setup 1.53ms gather-xref 0.00ms xref-setup 0.00ms cleanup 5.67ms
11-19 09:29:00.569 D/Mono    ( 2812): GC_BRIDGE: Complete, was running for 34.23ms
11-19 09:29:00.569 D/Mono    ( 2812): GC_MINOR: (Concurrent start) time 49.75ms, stw 91.64ms promoted 1186K major size: 26320K in use: 24598K los size: 36360K in use: 35129K
11-19 09:29:00.569 D/Mono    ( 2812): GC_MAJOR_CONCURRENT_START: (Minor allowance)
Thread started: <Thread Pool> #76
Thread started: <Thread Pool> #77
Thread started: <Thread Pool> #78
Thread started: <Thread Pool> #79
Thread started: <Thread Pool> #80
Thread started: <Thread Pool> #81
Thread started: <Thread Pool> #82
Thread started: <Thread Pool> #83
Thread started: <Thread Pool> #84
Thread started: <Thread Pool> #85
Thread started: <Thread Pool> #86
Thread started: <Thread Pool> #87
Thread started: <Thread Pool> #88
Thread started: <Thread Pool> #89
Thread started: <Thread Pool> #90
Thread started: <Thread Pool> #91
Thread started: <Thread Pool> #92
Thread started: <Thread Pool> #93
Thread started: <Thread Pool> #94
Thread started: <Thread Pool> #95
11-19 09:29:01.699 I/zygote64( 2812): Explicit concurrent copying GC freed 2884(555KB) AllocSpace objects, 0(0B) LOS objects, 59% free, 4MB/10MB, paused 373us total 24.236ms
11-19 09:29:01.702 D/Mono    ( 2812): GC_TAR_BRIDGE bridges 760 objects 5665 opaque 1666 ABCs 758 ABCs-bridged 716 ABCs-visible 716 xref 55 cache-hit 0 cache-semihit 0 cache-miss 42 setup 0.05ms tarjan 2.06ms scc-setup 0.16ms gather-xref 0.03ms xref-setup 0.04ms cleanup 0.42ms
11-19 09:29:01.702 D/Mono    ( 2812): GC_BRIDGE: Complete, was running for 31.90ms
11-19 09:29:01.702 D/Mono    ( 2812): GC_MAJOR_CONCURRENT_FINISH: (finishing) time 1174.60ms, stw 21.14ms los size: 25000K in use: 20597K
11-19 09:29:01.702 D/Mono    ( 2812): GC_MAJOR_SWEEP: major size: 22976K in use: 18662K
11-19 09:29:02.862 D/Mono    ( 2812): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 ABCs 0 ABCs-bridged 0 ABCs-visible 716 xref 55 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.05ms tarjan 2.06ms scc-setup 0.16ms gather-xref 0.03ms xref-setup 0.04ms cleanup 0.00ms
11-19 09:29:02.863 D/Mono    ( 2812): GC_BRIDGE: Complete, was running for 0.12ms
11-19 09:29:02.863 D/Mono    ( 2812): GC_MINOR: (Nursery full) time 10.50ms, stw 411.05ms promoted 847K major size: 23104K in use: 19526K los size: 27840K in use: 23530K
11-19 09:29:03.049 D/Mono    ( 2812): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 ABCs 0 ABCs-bridged 0 ABCs-visible 716 xref 55 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.05ms tarjan 2.06ms scc-setup 0.16ms gather-xref 0.03ms xref-setup 0.04ms cleanup 0.00ms
11-19 09:29:03.049 D/Mono    ( 2812): GC_BRIDGE: Complete, was running for 0.08ms
11-19 09:29:03.049 D/Mono    ( 2812): GC_MINOR: (Nursery full) time 10.30ms, stw 124.74ms promoted 1100K major size: 23104K in use: 20627K los size: 27840K in use: 24615K
11-19 09:29:03.056 I/Choreographer( 2812): Skipped 157 frames!  The application may be doing too much work on its main thread.
11-19 09:29:03.074 D/InputMethodManager( 2812): HSIFW - flag : 0 Pid : 2812
[0:] no non-static method "Landroid/support/v7/widget/AppCompatButton;.setOutlineAmbientShadowABC(I)V": Cannot set property on attached control. Error: 
11-19 09:29:03.751 D/ViewRootImpl@5d456e4[MainActivity]( 2812): ViewPostIme pointer 0
11-19 09:29:03.767 W/zygote64( 2812): Checksum mismatch for dex base.apk
11-19 09:29:03.767 W/zygote64( 2812): Checksum mismatch for dex base.apk
11-19 09:29:03.810 D/ViewRootImpl@5d456e4[MainActivity]( 2812): ViewPostIme pointer 1
11-19 09:29:03.940 F/        ( 2812): * Assertion at /Users/builder/jenkins/workspace/archive-mono/2019-06/android/release/mono/metadata/sgen-tarjan-bridge.c:1140, condition `xref_count == xref_index' not met, function:processing_build_callback_data, xref_count is 61 but we added 59 xrefs
11-19 09:29:03.941 F/libc    ( 2812): Fatal signal 6 (SIGABRT), code -6 in tid 2943 (Thread Pool Wor)


代码

    System.Collections.Concurrent.ConcurrentStack<MyClass1> myobjs = new System.Collections.Concurrent.ConcurrentStack<MyClass1>();

    System.Threading.Tasks.Parallel.ForEach(viewModel.Searchobjs, (item) =>
    {
        if (item.objID != obj.objID)
        {

                myobjs.Push(new MyClass1(item, diff.Diff));

        }
    });
    if (myobjs.Count > 0)
    {
        
            List<MyClass1> closeobjs = myobjs.OrderBy(x => x.Diff).ToList();
            foreach (var item in closeobjs)
            {
                if (!objs.ContainsKey(item.objID))
                {
                    objs.Add(item.objID, item);
                    break;
                }
            }
    
        
    }

我注意到这可能是由于使用 ParallelTask 造成的,它可能会消耗更多内存并产生更多需要释放的垃圾。

我也在同一代码领域使用 Dictionary

相关问题

重现步骤

不确定

预期行为

不应从单声道崩溃

实际行为

崩溃

我也在 Xamarin.Forms/Github 上提出了问题。如果有人遇到这个问题并解决了,请帮助。

相关:https://github.com/mono/mono/issues/9314

如果有人遇到同样的问题,解决方案是将调试器类型从 Tarjan 切换到 New

  • 创建文件Environment.txt并将属性Build Action设置为AndroidEnvironment

  • 内容将是

MONO_GC_PARAMS=bridge-implementation=new,nursery-size=128‌​m,soft-heap-limit=51‌​2m
  • 确保您的 android 项目 .csproj 有类似
  • 的行
  <ItemGroup>
    <AndroidEnvironment Include="Environment.txt" />
  </ItemGroup>

请注意,我必须延长软堆限制,因为我的应用程序的默认值很低

  • 运行 应用程序