我一直在努力解决内存泄漏问题,似乎让免费对象无法释放。
最近我的webapp在w3wp.exe的内存达到1100 MB后开始崩溃。在事件日志中,我发现数据库连接超时错误,因为连接池已满(我将其设置为300个连接)。
使用windbg,做一个dumpheap -stat显示有很多未发布的'Free'类型的对象:
00000071f70ac438 10300 478192918免费
所以10300个免费对象占用了近500MB的空间。
对几个更大的自由对象(加上大小)做一个!gcroot给了我这些:
!gcroot 00000071f7d63b78+0x509778
Thread 1bf4:
000000765f43caf0 000007faab4a7f13 System.Data.SqlClient.SqlCommand..ctor(System.Data.SqlClient.SqlCommand)
rsi:
-> 00000071f6cfd0f8 System.Data.SqlClient.SqlCommand
-> 00000071f6cff710 System.Data.SqlClient.SqlParameterCollection
-> 00000071f6cff738 System.Collections.Generic.List`1[[System.Data.SqlClient.SqlParameter, System.Data]]
-> 00000071f6cff760 System.Object[]
-> 00000071f6cff7a0 System.Data.SqlClient.SqlParameter
-> 00000071f826d2f0 System.String
000000765f43cc60 000007faa65f5832 System.Data.EntityClient.EntityCommandDefinition..ctor(System.Data.Common.DbProviderFactory, System.Data.Common.CommandTrees.DbCommandTree)
rbp+8: 000000765f43cc98 (pinned)
-> 00000071f83aef08 System.Collections.Generic.List`1[[System.Data.Query.PlanCompiler.ProviderCommandInfo, System.Data.Entity]]
-> 00000071f83aef30 System.Object[]
-> 00000071f83aeee0 System.Data.Query.PlanCompiler.ProviderCommandInfo
-> 00000071f6cfc698 System.Data.Common.CommandTrees.DbQueryCommandTree
-> 00000071f6cfc5b0 System.Data.Common.CommandTrees.DbProjectExpression
-> 00000071f6cf5f98 System.Data.Common.CommandTrees.DbExpressionBinding
-> 00000071f6cf5f40 System.Data.Common.CommandTrees.DbSortExpression
-> 00000071f6cf5de8 System.Data.Common.CommandTrees.DbExpressionBinding
-> 00000071f6cf5cd8 System.Data.Common.CommandTrees.DbProjectExpression
-> 00000071f6ced718 System.Data.Common.CommandTrees.DbExpressionBinding
-> 00000071f6ced600 System.Data.Common.CommandTrees.DbJoinExpression
-> 00000071f6ceb3f0 System.Data.Common.CommandTrees.DbExpressionBinding
-> 00000071f6ceb398 System.Data.Common.CommandTrees.DbLimitExpression
-> 00000071f6ceb340 System.Data.Common.CommandTrees.DbFilterExpression
-> 00000071f6ceb310 System.Data.Common.CommandTrees.DbAndExpression
-> 00000071f6ceb2e0 System.Data.Common.CommandTrees.DbComparisonExpression
-> 00000071f6ceb2b8 System.Data.Common.CommandTrees.DbParameterReferenceExpression
-> 00000071f826d2f0 System.String
000000765f43cc60 000007faa65f5832 System.Data.EntityClient.EntityCommandDefinition..ctor(System.Data.Common.DbProviderFactory, System.Data.Common.CommandTrees.DbCommandTree)
rbp+190: 000000765f43ce20
-> 00000071f6ce9fb0 System.Data.Common.CommandTrees.DbQueryCommandTree
-> 00000071f6ce9f80 System.Data.Common.CommandTrees.DbLimitExpression
-> 00000071f6ce9e98 System.Data.Common.CommandTrees.DbProjectExpression
-> 00000071f6ce8100 System.Data.Common.CommandTrees.DbExpressionBinding
-> 00000071f6ce78f0 System.Data.Common.CommandTrees.DbFilterExpression
-> 00000071f8273be8 System.Data.Common.CommandTrees.DbAndExpression
-> 00000071f8273bb8 System.Data.Common.CommandTrees.DbComparisonExpression
-> 00000071f826d320 System.Data.Common.CommandTrees.DbParameterReferenceExpression
-> 00000071f826d2f0 System.String
000000765f43cf70 000007faa655da7d System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(System.Nullable`1<System.Data.Objects.MergeOption>)
r12:
-> 00000071f6ce2768 System.Data.Objects.ELinq.ELinqQueryState
-> 00000071f6ce7aa0 System.Data.Objects.ObjectParameterCollection
-> 00000071f6ce7ad0 System.Collections.Generic.List`1[[System.Data.Objects.ObjectParameter, System.Data.Entity]]
-> 00000071f6ce7af8 System.Object[]
-> 00000071f8273b80 System.Data.Objects.ObjectParameter
-> 00000071f826d2f0 System.String
似乎有些datacontext与EF被固定在某个地方。问题是:在哪里以及为什么?我的webapp充满了电子邮件。
答案 0 :(得分:0)
我不太确定你的具体情况(我可能需要看到更多的代码来获得更好的想法)但是很多人因为他们持有上下文太长时间而导致内存泄漏。 EF保留其先前在上下文生命周期内查询的所有实体的快照,以进行更改跟踪。
我要注意的是你在查询后直接处理你的上下文,如果你在上下文中使用using
块,这应该这样做。你拥有大量内存并且你的连接池已满的事实告诉我,你可能在某个地方创建了一个从未被处理过的上下文。