title: DbContext memory management
body: <p>I have a generic repository that i used to inject in a <code>Job</code> class on the start of the application. This job class intend to run forever in a loop, so the context that repository uses is created just once. The problem is after some days the application was consuming impressives 1gb of memory (in the beggining of the execution it consumes 40mb).</p>
  
  <p>After that, I implemented the <code>Dispose</code> of the <code>DBContext</code> class in the <code>Repository</code> and start to instantiate it in every loop. The problem is solved, but i lost the DIP principle. Here´s the new code:</p>
  
  <pre><code>public class Job
  {
  
     do{
          //some code
          using (var repository = new Repository&lt;User&gt;())
          {
              repository.Save(User);
          }
       }while(true);
  } 
  </code></pre>
  
  <p>The repository:</p>
  
  <pre><code>    public void Save(T entity)
      {
          DbSet.Add(entity);
          _context.SaveChanges();
      }
  
      private IDbSet&lt;TEntity&gt; DbSet
      {
          get { return _context.CreateSet&lt;TEntity&gt;(); }
      }
  </code></pre>
  
  <p>Is there some workaround or tip to make the context not consume this memory, instantiating it just once? Or must I need to lost the IoC pattern in that case?</p>
  
  <p>Thanks.</p>