title: max string size vb.net
body: <p>I am need of a large string (eLines) to pass into a method of a type library I am using. </p>
  
  <pre><code>eLineID = sao.PutText(777, eLines)
  </code></pre>
  
  <p>I am unable to build the string without running out of memory, as I get this error:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.OutOfMemoryException' occurred
    in mscorlib.dll</p>
  </blockquote>
  
  <p>I am trying to store the contents of a 500+ mb file into string. I have tried multiple ways to do this. This reads all the lines into individual strings, then attempts to build a string using <a href="https://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.110).aspx" rel="nofollow">System.Text.StringBuilder</a> object</p>
  
  <pre><code>If (System.IO.File.Exists(filename)) Then
              Dim sB As StringBuilder = New StringBuilder()
              Dim test As String
              Dim allLines As String() = System.IO.File.ReadAllLines(filename)
              'Seperate into blocks
              For i = 0 To allLines.Count() Step 1
                  test = allLines(i)
                  If (test.IndexOf("\t") &gt; -1) Then
                      test.Replace("\t", "")
                  End If
                  sB.AppendLine(test)
              Next
          Else
              Console.WriteLine("File object could not find specified file")
          End If
  </code></pre>
  
  <p>Another attempt read the whole file with the <a href="https://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.110).aspx" rel="nofollow">System.IO.StreamReader</a> object to one string at once:</p>
  
  <pre><code>eLines = read.ReadToEnd()
  </code></pre>
  
  <p>Reading line by line with StreamReader.ReadLine() is too slow to tell if ever will eventually run out of memory. </p>
  
  <p>I know I have more than enough memory to do this (32 GB and running it never raises current memory usage by much more than 1 GB), so I was wondering what was causing the error and if there is any way I can override it. </p>