1

I have a .NET application that uses Entity Framework as its ORM. After running the application for a few hours, the application becomes unresponsive when the garbage collector runs and eventually starts to log "Out of Memory" errors.

I took a dump of the application and it mentions that that the EntityClient Objects are taking 95% of the memory.

Image from Dot Net profiler

On digging deep into references I see :

enter image description here

Can someone help me with why these EntityConnection objects are living forever? From the application point of view, we are not even creating these objects in the code. The framework might be creating these.

Sorry, but I am a .NET novice :( Any help is appreciated.

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Geek
  • 23,089
  • 20
  • 71
  • 85
  • 1
    Grab one of those instances and see what is holding it (repeat until you trace it back to your code). In all likelyhood, you're not disposing of some object(s). – xxbbcc Oct 30 '17 at 19:03
  • 1
    As usual: what's the life cycle of the the EF context(s)? When do you instantiate them and when are they disposed? – Gert Arnold Oct 30 '17 at 19:12
  • Hi @xxbbcc : I have added another screenshot with the fields. I also have a question why should I essentially dispose these objects off ? Shouldn't they be garbage collected themselves ? – Geek Oct 30 '17 at 19:12
  • 1
    How are you using the DbContext of the entity framework, do you properly dispose them after use? See https://stackoverflow.com/questions/15666824/entity-framework-and-context-dispose – Peter Bons Oct 30 '17 at 19:12
  • Sorry but why do I need to dispose these off ? Shouldn't they automatically be Garbage collected ? – Geek Oct 30 '17 at 19:13
  • 1
    @Geek If the GC cannot tell that you're done with something, those objects will stay around until you shut down your process. – xxbbcc Oct 30 '17 at 19:14
  • I agree @xxbbcc . I understand GC well (Java background :-) ). Novice to EntityFramework and .Net. If you can please do post a link to something where there is a description of dispose(). I think dispose() is like finalize() in Java which is called before the Object is Garbage collected. – Geek Oct 30 '17 at 19:17
  • @Geek Read about `IDisposable` and look up EF tutorials that show how to use the DB context. When something implements `IDisposable`, you're supposed to clean it up when you don't need it anymore because it tells you it's using unmanaged resources. – xxbbcc Oct 30 '17 at 19:19
  • Thanks Dude @xxbbcc. I am beginning to understand. – Geek Oct 30 '17 at 19:21
  • @xxbbcc : Jesus the damn thing looks like it is the Model of MVC and is being used to store configuration tables. I can't dispose that off :-( – Geek Oct 30 '17 at 19:50
  • 1
    @Geek Without seeing your code it's impossible to tell what's wrong but likely something needs changing. You shouldn't have any DB code in your MVC layer - it should all be separated off to a business layer / repository. The MVC layer shouldn't need to know anything about a database. – xxbbcc Oct 30 '17 at 19:52
  • We need you to show us what happens inside your context class. It looks like some static/singleton event handler is tied to connection events. And please refer to my previous comment. – Gert Arnold Oct 31 '17 at 08:03
  • Thanks @GertArnold I am still searching. Can you give me an idea as to how EntityConnection and DBContext are related. This might be a obvious answer for some but I am still figuring out. Thanks for your support. – Geek Oct 31 '17 at 10:16
  • I can't start elaborating on broad issues how several moving parts are interacting without knowing what you need *specifically*. Why don't you follow up on repeated suggestions to show your code? – Gert Arnold Oct 31 '17 at 12:31
  • @GertArnold : Ya it is too broad. Let me see if I can put some snippets around. The code is too widespread and you might have guessed that I am not too well versed with it. I am probably asking very basic questions. Thanks for your time. – Geek Oct 31 '17 at 14:47

0 Answers0