title: COMToCLRDispatchHelper causing performance issues due to high % Time in GC
body: <p>I am running into a performance issue in my managed .net application.
  I am using COM to interface to an unmanaged library and during run time I notice in Process Explorer that my % Time in GC is really high most of the time, > 90%.</p>
  
  <p>I used VS 2012 memory analysis and it shows that most of my memory allocations are going on inside a .NET method called COMToCLRDispatchHelper. Basically over 98% of all the memory allocated by my application is being done in this method, and I would like to know what can be done to get around this as this is tanking my application performance.</p>
  
  <p>On further investigation I suspect that the high % in GC time is caused by the unmanaged library frequently making calls back into my managed code through a COM interface which has a signature like this: </p>
  
  <pre><code>int Read([Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] data, uint size);
  </code></pre>
  
  <p>I don't quite know how MarshalAs works, but I suspect that the unmanaged code allocates an array then it gets copied into a managed array, then the contents of it get copied back into the unmanaged array and the managed array is garbage collected.</p>
  
  <p>I don't have control over the unmanaged code to try to change this interface.
  Is there any thing I can do in the managed code that implements this Read method to relieve GC pressure? Or perhaps change the signature of this method in some way that it relieves GC pressure but still satisfies the unmanaged signature of this interface?</p>