title: Crash on Android arising from mono for using ParallelTask and ConcurrentStack usage
body: <h3>Description</h3>
  <p>I am getting a crash from mono, from the logo I dont get anything specific which I can change in the code</p>
  <pre><code>[] * 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)
  </code></pre>
  <p>Not sure what is the root cause because same code works fine for 2 iterations. I wrapped everything in try catch to see if there is some exception that I can catch. It seems to be issue with Garbage collector</p>
  <p>More log</p>
  <pre><code>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: &lt;Thread Pool&gt; #50
  The thread 0x32 has exited with code 0 (0x0).
  Thread finished: &lt;Thread Pool&gt; #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: &lt;Thread Pool&gt; #66
  Thread started: &lt;Thread Pool&gt; #67
  Thread started: &lt;Thread Pool&gt; #68
  Thread started: &lt;Thread Pool&gt; #69
  Thread started: &lt;Thread Pool&gt; #70
  Thread started: &lt;Thread Pool&gt; #71
  Thread started: &lt;Thread Pool&gt; #72
  Thread started: &lt;Thread Pool&gt; #73
  Thread started: &lt;Thread Pool&gt; #74
  Thread started: &lt;Thread Pool&gt; #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 &quot;Landroid/support/v7/widget/AppCompatButton;.setOutlineAmbientShadowABC(I)V&quot;: 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: &lt;Thread Pool&gt; #53
  The thread 0x35 has exited with code 0 (0x0).
  Thread finished: &lt;Thread Pool&gt; #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: &lt;Thread Pool&gt; #76
  Thread started: &lt;Thread Pool&gt; #77
  Thread started: &lt;Thread Pool&gt; #78
  Thread started: &lt;Thread Pool&gt; #79
  Thread started: &lt;Thread Pool&gt; #80
  Thread started: &lt;Thread Pool&gt; #81
  Thread started: &lt;Thread Pool&gt; #82
  Thread started: &lt;Thread Pool&gt; #83
  Thread started: &lt;Thread Pool&gt; #84
  Thread started: &lt;Thread Pool&gt; #85
  Thread started: &lt;Thread Pool&gt; #86
  Thread started: &lt;Thread Pool&gt; #87
  Thread started: &lt;Thread Pool&gt; #88
  Thread started: &lt;Thread Pool&gt; #89
  Thread started: &lt;Thread Pool&gt; #90
  Thread started: &lt;Thread Pool&gt; #91
  Thread started: &lt;Thread Pool&gt; #92
  Thread started: &lt;Thread Pool&gt; #93
  Thread started: &lt;Thread Pool&gt; #94
  Thread started: &lt;Thread Pool&gt; #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 &quot;Landroid/support/v7/widget/AppCompatButton;.setOutlineAmbientShadowABC(I)V&quot;: 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)
  
  
  </code></pre>
  <h1>Code</h1>
  <pre class="lang-cs prettyprint-override"><code>    System.Collections.Concurrent.ConcurrentStack&lt;MyClass1&gt; myobjs = new System.Collections.Concurrent.ConcurrentStack&lt;MyClass1&gt;();
  
      System.Threading.Tasks.Parallel.ForEach(viewModel.Searchobjs, (item) =&gt;
      {
          if (item.objID != obj.objID)
          {
  
                  myobjs.Push(new MyClass1(item, diff.Diff));
  
          }
      });
      if (myobjs.Count &gt; 0)
      {
          
              List&lt;MyClass1&gt; closeobjs = myobjs.OrderBy(x =&gt; x.Diff).ToList();
              foreach (var item in closeobjs)
              {
                  if (!objs.ContainsKey(item.objID))
                  {
                      objs.Add(item.objID, item);
                      break;
                  }
              }
      
          
      }
  </code></pre>
  <p>I noticed this might have caused by using <code>ParallelTask</code> which might be consuming more memory and creating more garbage to be freed.</p>
  <p>Also I am using a <code>Dictionary</code> in the same arena of code</p>
  <p>Related issue</p>
  <h3>Steps to Reproduce</h3>
  <p>Not exactly sure</p>
  <h3>Expected Behavior</h3>
  <p>Should not crash from mono</p>
  <h3>Actual Behavior</h3>
  <p>Crashing</p>
  <p>I have raised issue at <a href="https://github.com/xamarin/Xamarin.Forms/issues/8414" rel="nofollow noreferrer">Xamarin.Forms/Github</a> as well. If anyone has encountered this issue and resolved it please help.</p>
  
  <p>Relevant : <a href="https://github.com/mono/mono/issues/9314" rel="nofollow noreferrer">https://github.com/mono/mono/issues/9314</a></p>