title: Much memory fragmentation with System.Threading.OverlappedData (async await)
body: <p>I'm constantly running slowly out of memory. I've used WinDbg to have a look at the memory dump and this it what it looks like:</p>
  
  <pre class="lang-none prettyprint-override"><code>000007f975ee0630   557377     41027736 System.Object[]
  000007f975f004d0    18781     47746604 System.Byte[]
  000007f975efc358   561433     54013658 System.String
  000000000137b170     7616   1322123700      Free
  
  Total 7627101 objects
  Fragmented blocks larger than 0.5 MB:
          Addr     Size      Followed by        
  000000004a9f62b0   18.2MB 000000004bc28050 System.Threading.OverlappedData
  000000004dc2ce68   16.1MB 000000004ec522e8 System.Threading.OverlappedData
  0000000050adaec0   10.3MB 0000000051525620 System.Threading.OverlappedData
  000000005d47fd98   10.2MB 000000005deab618 System.Threading.OverlappedData
  0000000071718ab8   23.0MB 0000000072e13a80 System.Threading.OverlappedData
  0000000072e13e50   11.8MB 00000000739e4898 System.ServiceModel.Channels.SocketConnection
  00000000801a7830   29.7MB 0000000081f5dd60 System.Threading.OverlappedData
  000000008264ab58   14.0MB 000000008344bac0 System.Threading.OverlappedData
  000000008344bb30   11.6MB 0000000083fecf80 System.Threading.OverlappedData
  0000000083fecff0   13.6MB 0000000084d8dae8 System.Threading.OverlappedData
  0000000084d8db58  148.3MB 000000008e1d65f8 System.Threading.OverlappedData
  0000000093db04e0   19.4MB 00000000951158b8 System.Threading.OverlappedData
  0000000095115928   33.9MB 00000000972f2620 System.Threading.OverlappedData
  00000000abaa6738   71.9MB 00000000b0285cb0 System.Byte[]
  </code></pre>
  
  <p>Note that there are many more items in fragmented heap, I've only pasted the ones over 10MB. You can tell that the main problem is System.Threading.OverlappedData.</p>
  
  <p>Recently I've refactored all the code to use async &amp; await to something like this:</p>
  
  <pre><code>await Task.Factory.FromAsync&lt;string, MessageSender&gt;(
                                    this.MessageFactory.BeginCreateMessageSender, 
                                    this.MessageFactory.EndCreateMessageSender, 
                                    this.Topic.Path,
                                    null)
        .ConfigureAwait(false);
  </code></pre>
  
  <p>There is a lot of socket inbound (socket) and outbound (azure service bus) communication. How to avoid such memory fragmentation? </p>