title: Windows service consuming a lot of memory
body: <p>I have a windows service that on the OnStart method, calls a method that has an infinite loop. Everything works fine but after some time the used memory goes really high(around 2 GB). This windows service process hundred of records per second so it might not be a problem, but when you start it the first time the memory consumption is fine, it is just after some days it goes high.</p>
  <p>I'm trying to find is there a place where I need to dispose of an object but I'm out of ideas.
  Here is the code:</p>
  <hr />
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.ServiceProcess;
  using System.Linq;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Configuration;
  using Library;
  using Library.Classes;
  
  namespace Events
  {
      public partial class ServiceEvent : ServiceBase
      {
          string CT_BACK_GND_PROCESS_TIME = &quot;BackGndProcessTime&quot;;
          string CT_ParallelismType = &quot;ParallelismType&quot;;
  
          readonly static string CT_ProcessingMethod = &quot;ProcessingMethod&quot;;
          readonly static string CT_SmartProcessingAmount = &quot;SmartProcessingAmount&quot;;
          readonly static string CT_ParallelismCap = &quot;ParallelismCap&quot;;
  
          int minUnitId;
          int maxUnitId;
          string idGuid;
          int execType;
          int versionType;
          bool _IsStop = false;
          Functions library;
          Thread threadEvents;
  
          public ServiceEvent()
          {
              InitializeComponent();
              eventLog = new System.Diagnostics.EventLog();
              library = new Functions();
          }
  
          #region Window Service Events
  
          protected override void OnStart(string[] args)
          {
              eventLog.Log = &quot;Events records&quot;;
              eventLog.Source = &quot;Events records&quot;;
              eventLog.WriteEntry(&quot;WS Events On Start&quot;);
  
              Functions.SetFakeHttpContext();
  
              //Caching the Ids
              library.GetIds();
  
              minUnitId = Convert.ToInt32(ConfigurationManager.AppSettings[&quot;MinUnitId&quot;]);
              maxUnitId = Convert.ToInt32(ConfigurationManager.AppSettings[&quot;MaxUnitId&quot;]);
  
              execType = Convert.ToInt32(ConfigurationManager.AppSettings[&quot;ExecType&quot;]); //0 - Sequencial | 1 - Parallel
              threadEvents = new Thread(new ThreadStart(ProcessEvents));
              
              threadEvents.IsBackground = true;
              threadEvents.Start();
          }
  
          protected override void OnStop()
          {
              eventLog.WriteEntry(&quot;WS Events On Stop&quot;);
              this._IsStop = true;
          }
  
          #endregion
  
          #region Main Methods
  
          public void ProcessEvents()
          {
              // do this loop forever (or until the service is stopped)
              while (!this._IsStop)
              {
                  try
                  {
                      Guid guid = Guid.NewGuid();
                      idGuid = guid.ToString();
  
                      int sleepingTime = Convert.ToInt32(ConfigurationManager.AppSettings[CT_BACK_GND_PROCESS_TIME]);
  
                         
                      List&lt;EventInfo&gt; eventList = library.GetAllEventsInfo(minUnitId, maxUnitId);
                      //There are some events to process
  
                      if (eventList.Count &gt; 0)
                      {
                          Functions.LogForTimingDebugging(minUnitId.ToString() + &quot; | &quot; + idGuid + &quot;| WS PROCESS_LOCKSTATE_EVENTS&quot;, &quot;BEGIN&quot;, DateTime.Now.ToString(&quot;MM/dd/yyyy hh:mm:ss.fff&quot;)
                              + &quot; | COUNT: &quot; + eventList.Count.ToString()); //Processing time - new debug log
                          
                          var uniqueIds = eventList.Select(x =&gt; x.UnitId).Distinct().ToList();
                          long minId = eventList.Min(x =&gt; x.IdEvent);
                          long maxId = eventList.Max(x =&gt; x.IdEvent);
  
                          int processingMethod = Convert.ToInt32(ConfigurationManager.AppSettings[CT_ProcessingMethod]);
  
                          if (processingMethod == 0) // Normal
                          {
                              if (execType == 1) //In Parallel
                              {
                                  Parallel.ForEach(uniqueIds, (id) =&gt;
                                  {
                                      ProcessOneUnit(id, eventList);
                                  });
                              }
                              else if (execType == 0) //Sequencial
                              {
                                  foreach (var id in uniqueIds)
                                  {
                                      ProcessOneUnit(id, eventList);
                                  }
                              }
                          }
                          else if (processingMethod == 1) //In parallel with cap
                          {
                              int parallelismCap = Convert.ToInt32(ConfigurationManager.AppSettings[CT_ParallelismCap]);
  
                              if (uniqueIds.Count &gt; parallelismCap)
                              {
                                  int iterations = (uniqueIds.Count + parallelismCap - 1) / parallelismCap;
  
                                  for (int i = 0; i &lt; iterations; i++)
                                  {
                                      int currentPos = i * parallelismCap;
                                      int count = (currentPos + parallelismCap &gt; uniqueIds.Count ? uniqueIds.Count - currentPos : parallelismCap);
                                      var subList = uniqueIds.GetRange(currentPos, count);
                                      Parallel.ForEach(subList, (id) =&gt;
                                      {
                                          ProcessOneUnit(id, eventList);
                                      });
                                  }
                              }
                              else
                              {
                                  if (execType == 1) //In Parallel
                                  {
                                      Parallel.ForEach(uniqueIds, (id) =&gt;
                                      {
                                          ProcessOneUnit(id, eventList);
                                      });
                                  }
                                  else if (execType == 0) //Sequencial
                                  {
                                      foreach (var id in uniqueIds)
                                      {
                                          ProcessOneUnit(id, eventList);
                                      }
                                  }
                              }
                          }
                          else if (processingMethod == 2) //Smart processing
                          {
                              int smartProcessingAmount = Convert.ToInt32(ConfigurationManager.AppSettings[CT_SmartProcessingAmount]);
  
                              if (eventList.Count &gt;= smartProcessingAmount)
                              {
                                  Parallel.ForEach(uniqueIds, (id) =&gt;
                                  {
                                      ProcessOneUnit(id, eventList);
                                  });
                              }
                              else
                              {
                                  if (execType == 1) //In Parallel
                                  {
                                      Parallel.ForEach(uniqueIds, (id) =&gt;
                                      {
                                          ProcessOneUnit(id, eventList);
                                      });
                                  }
                                  else if (execType == 0) //Sequencial
                                  {
                                      foreach (var id in uniqueIds)
                                      {
                                          ProcessOneUnit(id, eventList);
                                      }
                                  }
                              }
                          }
                          else if (processingMethod == 3) //Smart processing with cap
                          {
                              int smartProcessingAmount = Convert.ToInt32(ConfigurationManager.AppSettings[CT_SmartProcessingAmount]);
  
                              if (eventList.Count &gt;= smartProcessingAmount)
                              {
                                  int parallelismCap = Convert.ToInt32(Functions.GetContentTemplate(CT_ParallelismCap, 0));
  
                                  if (uniqueIds.Count &gt; parallelismCap)
                                  {
                                      int iterations = (uniqueIds.Count + parallelismCap - 1) / parallelismCap;
  
                                      for (int i = 0; i &lt; iterations; i++)
                                      {
                                          int currentPos = i * parallelismCap;
                                          int count = (currentPos + parallelismCap &gt; uniqueIds.Count ? uniqueIds.Count - currentPos : parallelismCap);
                                          var subList = uniqueIds.GetRange(currentPos, count);
                                          Parallel.ForEach(subList, (id) =&gt;
                                          {
                                              ProcessOneUnit(id, eventList);
                                          });
                                      }
                                  }
                                  else
                                  {
                                      Parallel.ForEach(uniqueIds, (id) =&gt;
                                      {
                                          ProcessOneUnit(id, eventList);
                                      });
                                  }
  
                              }
                              else
                              {
                                  if (execType == 1) //In Parallel
                                  {
                                      Parallel.ForEach(uniqueIds, (id) =&gt;
                                      {
                                          ProcessOneUnit(id, eventList);
                                      });
                                  }
                                  else if (execType == 0) //Sequencial
                                  {
                                      foreach (var id in uniqueIds)
                                      {
                                          ProcessOneUnit(id, eventList);
                                      }
                                  }
                              }
                          }
  
                          library.DeleteMultiplesEventsInfo(minUnitId, maxUnitId, minId, maxId);
  
                          Functions.LogForTimingDebugging(minUnitId.ToString() + &quot; | &quot; + idGuid + &quot;| WS PROCESS_EVENTS&quot;, &quot;END&quot;, DateTime.Now.ToString(&quot;MM/dd/yyyy hh:mm:ss.fff&quot;)); //Processing time - new debug log
                      }
                      
                      Thread.Sleep(sleepingTime); //Wait for next iteration based on Global Content Template
                  }
                  catch (Exception ex)
                  {
                      Functions.SendEventLog(minUnitId.ToString() + &quot; | WS PROCESS_EVENTS&quot;, &quot;General Error&quot;, ex.ToString());
                  }
              }
              eventLog.WriteEntry(&quot;WS PROCESS_EVENTS SERVICE STOPPED | &quot; + DateTime.Now.ToString(&quot;MM/dd/yyyy hh:mm:ss.fff&quot;));
          }
  
          public void ProcessOneUnit(int id, List&lt;EventInfo&gt; eventList)
          {
              var unitEvents = new List&lt;EventInfo&gt;();
              unitEvents.AddRange(eventList.Where(x =&gt; x.UnitId == id).ToList());
  
              foreach (var item in unitEvents)
              {
                  library.ProcessOneEventInfo(item);
              }
          }
  
          #endregion
      }
  }
  </code></pre>
  <hr />