title: 'System.OutOfMemoryException' on copying csv file to Data table
body: <p>I have developed a code in C# that copies data from csv file to a data table. The csv file contains 5 Million rows and I read the rows line by line to prevent memory issues. I wonder why I still get OutOfMemory Exception. I added breakPoints to make sure the right strings are copied to my variables and they are working correctly. Any ideas?</p>
  
  <pre><code>int first_row_flag = 0;   //first row is column name and we dont need to import them
  string temp;                
  foreach (var row in File.ReadLines(path3))
  {
      if (!string.IsNullOrEmpty(row))
      {
          int i = 0;
          if (first_row_flag != 0)
          {
              dt.Rows.Add();
              foreach (string cell in row.Split(','))
              {
  
                  if (i &lt; 9)
                  {
                      temp = cell.Replace("\n", "");
                      temp = temp.Replace("\r", "");
                      dt.Rows[dt.Rows.Count - 1][i] = temp;
                      i++;
                  }
              }
          }
          else
          {
              first_row_flag++;    //get rid of first row
          }
      }
  }
  </code></pre>
  
  <p>The number of columns in each row is 9. Thats why I use i to make sure I will not read unexpected data in 10th column.</p>
  
  <p>Here is the stack trace:</p>
  
  <p><a href="https://i.stack.imgur.com/zjI33.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zjI33.jpg" alt="enter image description here"></a></p>