title: Windows service memory leak using TopShelf and Automapper c#
body: <p>I'm running a windows service using TopShelf (based on console app in C# .NET 4.6.1) and I'm using Automapper 9.0.0. Every 10 seconds I run a task that processes about 1000 rows in a Ms SQL database (using entity framework), It seems like Automapper is taking up a lot of memory, and the memory grows each time the task is run (In task manager I can see the service taking up over 3000 Meg of RAM++).</p>
  
  <p>I am new to Automapper and don't now if there is anything I need to code to release manually the memory. Somewhere I saw a huge amount of handlers and I was wondering if Automapper generates these handlers and how I can clean them up.</p>
  
  <p>I tried putting a <strong>GC.Collect()</strong> at the end of each task but I don't seem to see a difference</p>
  
  <p><a href="https://i.stack.imgur.com/EwMXw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EwMXw.png" alt="enter image description here"></a></p>
  
  <p>Here is a code extract of my task:</p>
  
  <pre><code>private void _LiveDataTimer_Elapsed(object sender, ElapsedEventArgs e)
          {
              // setting up Ninject data injection
              var kernel = new StandardKernel();
              kernel.Load(Assembly.GetExecutingAssembly());
              //var stationExtesions = kernel.Get&lt;IStationExtensionRepository&gt;();
              //var ops = kernel.Get&lt;IOPRepository&gt;();
              //var opExtensions = kernel.Get&lt;IOPExtensionRepository&gt;();
              //var periods = kernel.Get&lt;IPeriodRepository&gt;();
              //var periodExtensions = kernel.Get&lt;IPeriodExtensionRepository&gt;();
  
  
              // create the LiveDataTasks object
              //var liveDataTasks = new LiveDataTasks(stationExtesions, ops, opExtensions, periods, periodExtensions);
  
              // sync the station live data
              //liveDataTasks.SyncLiveStationData();
  
              // force garbage collection to prevent memory leaks
              //GC.Collect();
  
              Console.WriteLine("LiveDataTimer: Total available memory before collection: {0:N0}", System.GC.GetTotalMemory(false));
              System.GC.Collect();
              Console.WriteLine("LiveDataTimer: Total available memory collection: {0:N0}", System.GC.GetTotalMemory(true));
          }
  </code></pre>
  
  <p>MOFICATIONS: I added some console outputs at the end of the code displaying the TotalMemory used. I removed <code>GC.Collect()</code> because it doesn't change anything and commented out most of the code accessing database. Now I realize that <code>kernel.Load(Assembly.GetExecutingAssembly());</code> already makes memory grow very fast. See the following console capture:
  <a href="https://i.stack.imgur.com/oLnrX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oLnrX.png" alt="enter image description here"></a>
  Now if I comment out <code>kernel.Load(Assembly.GetExecutingAssembly());</code> I get a stable memory situation again. How can I Dispose or unload the Kernel???
  <a href="https://i.stack.imgur.com/QglQF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QglQF.png" alt="enter image description here"></a></p>