title: C# OutofMemoryException with Regex
body: <p>I'm getting a <code>OutOfMemoryException</code> at </p>
  
  <p><code>if (Regex.IsMatch(output, @"^\d"))</code></p>
  
  <p>But I'm unsure of what's causing it, my program had been running for like 4 minute. Reading text files (a lot of them). Bulk inserting them into SQL. The output string at the time contained nothing special, a small text read from a <code>.txt</code> file.</p>
  
  <p>I'm assuming this is happening because of the amount of times it needs to regex check, after 4 minute it was in the million times. Is there a way to prevent the Memory problem? dispose or clear before I start looping? If so how do you that?</p>
  
  <p><strong>EDIT:</strong>
  I'm not reading a big file, I'm reading a lot of files. At the time it failed it was around 6666~ files it already read (5 folders) but it needs to read 60 folders in total -> 80.361 .txt files</p>
  
  <p><strong>EDIT:</strong>
  Added the source code. Hoping to clarify</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>added:
  static void DisposeAll(IEnumerable set)</p>
  
  <pre><code>static void DisposeAll(IEnumerable set)
  {
      foreach (Object obj in set)
      {
          IDisposable disp = obj as IDisposable;
          if (disp != null) { disp.Dispose(); }
      }
  }
  </code></pre>
  
  <p>And I'm executing this at the end of each loop of a folder.</p>
  
  <pre><code>DisposeAll(ListExtraInfo);
  DisposeAll(ListFouten);
  ListFouten.Clear();
  ListExtraInfo.Clear();
  </code></pre>
  
  <p>Error placement changed, no longer the Regex but ListFouten is causing it now.
  Still happening at around 6666 .txt files read.</p>
  
  <blockquote>
    <p>Exception of type 'System.OutOfMemoryException' was thrown.</p>
  </blockquote>
  
  <pre><code>static void Main(string[] args)
          {
              string pathMMAP = @"G:\HLE13\Resultaten\MMAP";
              string[] entriesMMAP = Directory.GetDirectories(pathMMAP);
              List&lt;string&gt; treinNamen = new List&lt;string&gt;();
  
              foreach (string path in entriesMMAP)
              {
                  string TreinNaam = new DirectoryInfo(path).Name;
                  treinNamen.Add(TreinNaam);
                  int IdTrein = 0;
                  ListExtraInfo = new List&lt;extraInfo&gt;();
                  ListFouten = new List&lt;fouten&gt;();
                  readData(TreinNaam, IdTrein, path);
               }
          }
  
  
          static void readData(string TreinNaam, int IdTrein, string path)
          {
              using (SqlConnection sourceConnection = new SqlConnection(GetConnectionString()))
              {
                  sourceConnection.Open();
  
  
                  try
                  {
                      SqlCommand commandRowCount = new SqlCommand(
                   "SELECT TreinId FROM TestDatabase.dbo.Treinen where Name = " + TreinNaam,
                   sourceConnection);
                      IdTrein = Convert.ToInt16(commandRowCount.ExecuteScalar());
  
                  }
                  catch (Exception ex)
                  {
  
  
                  }
  
              }
  
              string[] entriesTreinen = Directory.GetDirectories(path);
              foreach (string rapport in entriesTreinen)
              {
  
                  string RapportNaam = new DirectoryInfo(rapport).Name;
                  FileInfo fileData = new System.IO.FileInfo(rapport);
  
                  leesTxt(rapport, TreinNaam, GetConnectionString(), IdTrein);
  
              }
          }
          public static string datum;
          public static string tijd;
          public static string foutcode;
          public static string absentOfPresent;
          public static string teller;
          public static string omschrijving;
          public static List&lt;fouten&gt; ListFouten;
          public static List&lt;extraInfo&gt; ListExtraInfo;
          public static string textname;
          public static int referentieGetal = 0;
  
  
          static void leesTxt(string rapport, string TreinNaam, string myConnection, int TreinId)
          {
              foreach (string textFilePath in Directory.EnumerateFiles(rapport, "*.txt"))
              {
  
                  textname = Path.GetFileName(textFilePath);
                  textname = textname.Substring(0, textname.Length - 4);
  
                  using (StreamReader r = new StreamReader(textFilePath))
                  {
                      for (int x = 0; x &lt;= 10; x++)
                          r.ReadLine();
  
                      string output;
  
                      Regex compiledRegex = new Regex(@"^\d", RegexOptions.Compiled);
                      string[] info = new string[] { };
                      string[] datumTijdelijk = new string[] { };
  
                      while (true)
                      {
  
                          output = r.ReadLine();
                          if (output == null)
                              break;
  
  
                          if (compiledRegex.IsMatch(output))
                          {
                              info = output.Split(' ');
                              int kolom = 6;
                              datum = info[0];
                              datumTijdelijk = datum.Split(new[] { '/' });
  
  
                              try
                              {
                                  datum = string.Format("{2}/{1}/{0}", datumTijdelijk);
                                  tijd = info[1];
                                  foutcode = info[2];
                                  absentOfPresent = info[4];
                                  teller = info[5];
                                  omschrijving = info[6];
                              }
                              catch (Exception ex)
                              {
  
                              }
  
  
                              while (kolom &lt; info.Count() - 1)
                              {
                                  kolom++;
                                  omschrijving = omschrijving + " " + info[kolom];
                              }
                              referentieGetal++;
  
  
                              ListFouten.Add(new fouten { Date = datum, Time = tijd, Description = omschrijving, ErrorCode = foutcode, Module = textname, Name = TreinNaam, TreinId = TreinId, FoutId = referentieGetal });
  
                          }
  
  
                          if (output == string.Empty)
                          {
                              output = " ";
                          }
                          if (Char.IsLetter(output[0]))
                          {
                              ListExtraInfo.Add(new extraInfo { Values = output, FoutId = referentieGetal });
                          }
  
                      }
  
                  }
  
              }
  
          }
  </code></pre>