开发者

Is the entity framework using up my memory?

开发者 https://www.devze.com 2023-01-21 10:34 出处:网络
I\'m trying to work out why memory usage in a basic application I\'m writing is excessive. I\'m trying to create a dynamic MVC sitemap with about 25000 nodes a开发者_如何学Cnd populating it from a dat

I'm trying to work out why memory usage in a basic application I'm writing is excessive. I'm trying to create a dynamic MVC sitemap with about 25000 nodes a开发者_如何学Cnd populating it from a database table using the entity framework (though this detail is probably not important)

I have the following code:

foreach (var c in context.Companies) { }

With a breakpoint before this line, webdev.webserver40.exe consumes about 50mb. Afterwards about 250mb. I have tried disposing the context, letting the context fall out of scope; calling GC.Collect() each time, yet I can't seem to recover this memory.

NB I know that memory doesn't have to be, and normally isn't released immediately, I just want to put my mind at ease that there isn't a memory leak here.

Thanks


I have a similar situation with almost exact same code, except my context is Entity Framework context, and data is comming from database. It appears that this is a "memory usage spike" and not a permanent "leak" as the process' overall memory consumption eventually goes back to a reasonable normal level. It is still not a good thing though.

It appears that it is loading the entire column data, including a large binary column data in my case, into memory. The data is kept until the "scope" ends (e.g. content goes out of scope). While looping, some of the data survives garbage collection, getting promoted to next generations, which causes retention period longer (in my case, memory gets released about 10 minutes later).

I tried various things but I think this is just a behavior we have to embrace.

In my particular case, I was using binary serialization to save object state in the database. I haven't fixed this yet but my workaround for reducing memory usage spike would be to refactor the code so that I don't use binary serialization but rather save primitive data into database table columns, and simply assign values to objects in codes. The next level of fix would be to use "lazy initialization", then caching, etc.

In your site map, maybe you can "segment" the view and load only subset of the nodes.


Why not fire up one of the commercially available memory profilers (all have free trials) such a MemProfiler, compare 2 snapshots and see where you memory went.

Without seeing your code, it's difficult to say whether you have a memory leak.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号