title: why do I get System.OutOfMemoryException even when there is about 700Mb of RAM free?
body: <p>I was running some test, to see the how my logging would perform is instead of doing <code>File.AppendAllText</code> I would first write to a memory stream and then copy to file. So, just to see how fast memory operation is I did this..</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
      {
          using (var memFile = new System.IO.MemoryStream())
          {
              using (var bw = new System.IO.BinaryWriter(memFile))
              {
                  for (int i = 0; i &lt; Int32.MaxValue; i++)
                  {
                      bw.Write(i.ToString() + Environment.NewLine);
                  }
                  bw.Flush();
              }
              memFile.CopyTo(new System.IO.FileStream(System.IO.Path.Combine("C", "memWriteWithBinaryTest.log"), System.IO.FileMode.OpenOrCreate));
          }
      }
  </code></pre>
  
  <p>When <code>i</code> reached <code>25413324</code> I got a <code>Exception of type 'System.OutOfMemoryException' was thrown.</code> even though my <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx" rel="nofollow noreferrer">Process Explorer</a> says I have about 700Mb of free ram???</p>
  
  <p>Here are the screen shots (just in case)</p>
  
  <p>Process Explorer
  <img src="https://i.stack.imgur.com/kOyL6.png" alt="enter image description here"></p>
  
  <p>Here's the winform</p>
  
  <p><img src="https://i.stack.imgur.com/dGudi.png" alt="enter image description here"></p>
  
  <p><strong>EDIT :</strong> For the sake of more objects being created on heap, I rewrote the <code>bw.write</code> to this</p>
  
  <pre><code>bw.Write(i);
  </code></pre>