title: .net collections memory optimization - will this method work?
body: <p>Just like almost any other big <code>.NET</code> application, my current <code>C#</code> project contains many <code>.net</code> collections .<br>
  Sometimes I don't know, from the beginning, what the size of a Collection (List/ObservableCollection/Dictionary/etc.) is going to be.<br>
  But there are many times when I do know what it is going to be.  </p>
  
  <p>I often get an <code>OutOfMemoryException</code> and I've been told it can happen not only because process size limits, but also because of fragmentation.  </p>
  
  <p>So my question is this - will setting collection's size (using the capacity argument in the constructor) every time I know its expected size help me prevent at least some of the fragmentation problems ?  </p>
  
  <p>This quote is from the <a href="http://msdn.microsoft.com/en-us/library/dw8e0z9z(v=VS.80).aspx" rel="nofollow noreferrer">msdn</a> : </p>
  
  <blockquote>
    <p>If the size of the collection can be
    estimated, specifying the initial
    capacity eliminates the need to
    perform a number of resizing
    operations while adding elements to
    the List.</p>
  </blockquote>
  
  <p>But still, I don't want to start changing big parts of my code for something that might not be the real problem.  </p>
  
  <p>Has it ever helped any of you to solve out of memory problems ?  </p>