title: How to reduce time spent in GC
body: <p>I'm creating a desktop application that has a compute-heavy operation that potentially runs for several seconds. Obviously there is a need to minimize the time of this operation. The operation is fairly easy to parallellize (individual subtasks), and each subtask takes around 50ms on a single thread. On multiple threads, each subtask takes 4-5 times as long because 40-50% time is spent in GC, effectively cancelling the speedup completely.</p>
  
  <p>So I need to give the GC less work. My first thought was to try to find which type of object was being garbage collected the most, but I realized that although I often do memory profiling, I had never searched for a pattern like this. Usually a look at heap snapshots, or differences between heap snapshots, but these show objects that are alive, not the objects that were created and disposed between those snapshots. So that is my first question: what is the easiest way to find which types are created and garbage collected the most? I tried looking for method call counts to see if some constructor was called suspiciously often, but all objects created in millions were only small struct types. These should have no effect on GC even if boxed if I understand things correctly?</p>
  
  <p>The algorithm creates hundreds of thousands of individual result point objects. These of course aren't supposed to be gc'd because they represent the output of the operation. But it leads me to my second question: is the time spent in GC mostly dependent on the total number of objects or mostly depending on the number of objects actually collected? Should I try to limit the number of result objects and instead use fewer but larger result objects?</p>
  
  <p>Edit: I found the time spent in GC by using the VS 2010 concurrency visualizer. Also, in the parallel piece of code most sections of blocked threads were waiting for gc</p>
  
  <p>Edit: I should clarify that the performance problem is because the execution is effectively serialized on the workstation GC. See for example the performance problem described in this post.</p>
  
  <p><a href="http://blogs.msdn.com/b/hshafi/archive/2010/06/17/case-study-parallelism-and-memory-usage-vs2010-tools-to-the-rescue.aspx" rel="nofollow">http://blogs.msdn.com/b/hshafi/archive/2010/06/17/case-study-parallelism-and-memory-usage-vs2010-tools-to-the-rescue.aspx</a></p>
  
  <p>I can't do anything about the garbage collector blocking my threads (and I don't think I want the server GC for a desktop app, correct?). So in order to get a linear speedup for this operation, I need to reduce the number of times the GC is invoked. Most of the time wasted is actually wasted by other threads blocked waiting for one thread to do GC.</p>