title: C# Error: OutOfMemoryException - Reading a large text file and replacing from dictionary
body: <p>I'm new to C# and object-oriented programming in general. I have an application which parses text file.</p>
  
  <p>The objective of the application is to read the contents of the provided text file and replace the matching values.</p>
  
  <p>When a file about 800 MB to 1.2GB is provided as the input, the application crashes with error System.OutofMemoryException. </p>
  
  <p>On researching, I came across couple of answers which recommend changing the <strong>Target Platform: to x64</strong>. </p>
  
  <p>Same issue exists after changing the target platform.</p>
  
  <p>Following is the code:</p>
  
  <pre><code>// Reading the text file
                  var _data = string.Empty;
                  using (StreamReader sr = new StreamReader(logF))
                  {
                      _data = sr.ReadToEnd();
                      sr.Dispose();
                      sr.Close();
                  }
  
                  foreach (var replacement in replacements)
                  {
                      _data = _data.Replace(replacement.Key, replacement.Value);
                  }
  
  
                  //Writing The text File
                  using (StreamWriter sw = new StreamWriter(logF))
                  {
                      sw.WriteLine(_data);
                      sw.Dispose();
                      sw.Close();
                  } 
  </code></pre>
  
  <p>The error points to  </p>
  
  <blockquote>
    <p>_data = sr.ReadToEnd();</p>
  </blockquote>
  
  <p>replacements is a dictionary. The Key contains the original word and the Value contains the word to be replaced.</p>
  
  <p>The Key elements are replaced with the Value elements of the KeyValuePair.</p>
  
  <p>The approached being followed is Reading the file, replacing and writing.</p>
  
  <p>I tried using a StringBuilder instead of string yet the application crashed.</p>
  
  <p>Can this be overcome by reading the file one line at a time, replacing and writing? What would be the efficient and faster way of doing the same. </p>
  
  <p>Update: The system memory is 8 GB and on monitoring the performance it spikes upto 100% memory usage.</p>
  
  <p>@Tim Schmelter answer works well.</p>
  
  <p>However, the memory utilization spikes over 90%. It could be due to the following code:</p>
  
  <pre><code>            String[] arrayofLine = File.ReadAllLines(logF);
              // Generating Replacement Information
              Dictionary&lt;int, string&gt; _replacementInfo = new Dictionary&lt;int, string&gt;();
              for (int i = 0; i &lt; arrayofLine.Length; i++)
              {
                  foreach (var replacement in replacements.Keys)
                  {
                      if (arrayofLine[i].Contains(replacement))
                      {
                          arrayofLine[i] = arrayofLine[i].Replace(replacement, masking[replacement]);
                          if (_replacementInfo.ContainsKey(i + 1))
                          {
                              _replacementInfo[i + 1] = _replacementInfo[i + 1] + "|" + replacement;
                          }
                          else
                          {
                              _replacementInfo.Add(i + 1, replacement);
                          }
                      }
                  }
              }
  
  //Creating Replacement Information
                  StringBuilder sb = new StringBuilder();
                  foreach (var Replacement in _replacementInfo)
                  {
                      foreach (var replacement in Replacement.Value.Split('|'))
                      {
                          sb.AppendLine(string.Format("Line {0}: {1} ---&gt; \t\t{2}", Replacement.Key, replacement, masking[replacement]));
                      }
                  }
  
                  // Writing the replacement information
                  if (sb.Length!=0)
                  { 
                  using (StreamWriter swh = new StreamWriter(logF_Rep.txt))
                  {
                      swh.WriteLine(sb.ToString());
                      swh.Dispose();
                      swh.Close();
                  }
                  }
                  sb.Clear();
  </code></pre>
  
  <p>It finds the line number in which the replacement was made. Can this be captured using Tim's code in order to avoid loading the data into memory multiple times.</p>