Entity Framework中的内存泄漏问题

mmruyue 2011-04-29 06:11:19
最近发现在entity framework中出现内存泄漏的问题。实体对象总是包含着ObjectContext的引用,即便在ObjectContext已经被Dispose的情况下。
通常我们都会使用“using”语句或者手动调用obj.dispose()来销毁ObjectContext。我希望在销毁该对象后,对应的内存空间会被回收。但实际我通过.net memory profiler查看内存对象的时候发现ObjectContext并没有被回收,上面提示“对象已经销毁但没有被垃圾回收”。原因是我需要将实体对象“user”存在session中(在session的有效期内,user对象不会被回收),而"user"包含了对"ObjectContext"的引用。
Users user = null; 
using (B2CEntities context = new B2CEntities())
{
  user = context.Users.First();
}
Session.Add(user.OID.ToString(), user);

user对象查出来后默认的状态是“Unchanged”,我发现如果手动调用context.detach(user), user的状态变为“Detached”,就不会引用context,也就不会产生内存泄漏的问题。然而这样会产生另外一个问题,detach后user中所有导航属性都会变成“Unload”。无疑这不是可行的方法。
不清楚这是否entity framework的Bug,各位大牛有何建议?
在msdn上也发了贴,请参考这里http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/eace1b85-a15b-48cc-aaa4-17cf85f1111c
...全文
386 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
glacierking 2012-05-08
  • 打赏
  • 举报
回复
你的考虑可能是多余的,内存不会泄漏,因为调用dispose()来销毁ObjectContext后,虽然objectContext可能被user引用不能放掉,但在ObjectContext里dispose()已经把有关的托管与非托管的资源全部释放掉了,只留自已的属性指针与方法表指针,占用的可能内存也就是,32位地址=4个字节,多个地址,也就几十个字节,这样在session的user中带有是没有任何关系的。如果你再新建个ObjectContext后,把session中的user指向它,那原来的ObjectContext,就会被回收掉。
mmruyue 2011-12-15
  • 打赏
  • 举报
回复
dispose跟回收内存确实不是一回事。
然而问题是,GC后也不能释放内存,因为还存在对该对象的引用,说明entity的dispose并没有恰当的销毁所有引用。
[Quote=引用 1 楼 sp1234 的回复:]
引用楼主 mmruyue 的回复:
通常我们都会使用“using”语句或者手动调用obj.dispose()来销毁ObjectContext。我希望在销毁该对象后,对应的内存空间会被回收


谁跟你说过dispose是回收内存空间?释放对象跟回收内存空间是两回事!
[/Quote]
PPMMOOT 2011-05-22
  • 打赏
  • 举报
回复
要考虑这么多么???
  • 打赏
  • 举报
回复
[Quote=引用楼主 mmruyue 的回复:]
通常我们都会使用“using”语句或者手动调用obj.dispose()来销毁ObjectContext。我希望在销毁该对象后,对应的内存空间会被回收
[/Quote]

谁跟你说过dispose是回收内存空间?释放对象跟回收内存空间是两回事!
This is definitely the book to have by your side if you are programming with the ADO.NET Entity Framework 1.0. The author touches on a ton of subjects that include: Data Binding with Windows Forms and WPF Applications, Using Stored Procedures with the EDM, LINQ to Entities Queries, Customizing Entities, Using the ASP.NET EntityDataSource Control, Using Entities with Web and WCF Services, Using the Entity Framework in n-Tier ASP.NET Applications and n-Tier Client-Side Applications, Handling Entity Framework Exceptions, Performance, Security, Multithreaded Applications, and much more. There is a lot covered in this book. One of the coolest things about this book is the amount of new possibilities it introduces. Many which I would not have considered without seeing them in this book. I also like that the book covers the architectural aspects of integrating EF into several different types of architectures. The author drills into each subject enough to get a thorough understand. With all the material covered, that is rare, but the book is an 800 page whopper. I have been working with the EF 1.0 since its release in August and can say that this book has more information jammed into it than all the other resources I have been using combined. The book includes VB.NET and C# code examples. The book has a support site (google for learnentityframework) with the code samples and the database scripts available. The downloaded code is also in both VB.NET and C#. It is well organized and very usable. The book is very well organized and is a good read. The author has a good writing style. All in all, I do not think you can do without this book if you are going to do anything besides play around with the Entity Framework.

8,494

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧