简体   繁体   中英

Entity Framework Memory Leak

I am monitoring the consumption of my software's memory through dotMemory JetBrains.

I noticed that when I do a query through my repository, then if I close the window, I have left the object from which I made the call; my repository into memory, I used using and even dispose of my context but nothing remains .. in memory. What can I check?

Here is the offending code:

LoginViewModel.cs

using (DbContext = new Context())
{
   var creazioneDocumentoRepository = new RepositoryBase<CreazioneDocumento>(ctx);
   var creazioneDocumento = creazioneDocumentoRepository.Lista();

   if (creazioneDocumento == null || creazioneDocumento.Count == 0) 
      return;

   var decimaliQuantita = creazioneDocumento.Max(x => x.NumeroDecimaliQuantita);
   _NumeroDecimaliQuantita = decimaliQuantita != 0 && decimaliQuantita > 0 ? decimaliQuantita : 0;

   var decimaliPrezzo = creazioneDocumento.Max(x => x.NumeroDecimaliPrezzo);
   _NumeroDecimaliPrezzo = decimaliPrezzo != 0 && decimaliPrezzo > 0 ? decimaliPrezzo : 3;
   _NumeroDecimaliImponibile = 2;

   //   ctx.Dispose();
}

Doing debugging, I noticed that up to:

if (creazioneDocumento == null || creazioneDocumento.Count == 0) return;

the object is not remembered, but as soon as I run the "max" the object remains in memory.

Here is a screen shot from dotmemory:

在此处输入图片说明

Instead this screenshot tells me what are the methods that reference and keep in memory my loginviewmodel and I believe they are the two max:

在此处输入图片说明

.NET CLR is a managed, garbage-collected runtime/virtual machine.

You can't expect that objects will be reclaimed and removed from memory as soon as they're not necessary.

In the other hand, IDisposable.Dispose is just an interface method to define code to release underlying resources used by the implementation: it's not a built-in method to release memory.

The garbage collector (GC) reclaims memory as a background process and it won't remove anything instantly, but based on assumptions overtime.

Further reading: Understanding garbage collection in .NET

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM