Strange memory leak related to DataContext and PropertyChanged event
I have a windows service written in C#.
After running for a couple of days it will be using about 2GB of ram.
Using ".NET Memory Profiler" I noticed that there is a hugely increasing number of instances of a certain T_TurbineEventLog class. These objects come from a loop that fetches them every minute through a LINQ-to-SQL query (lots of them each time), and on certain occations I keep a reference to few of them.
The memory profiler told me that most of these objects are only kept alive through a shortest root path resembling the following (from my memory):
root开发者_StackOverflow -> ... -> T_TurbineEventLog -> PropertyChanged -> ...(eventhandlers) -> IdentityManager -> ...Cache -> T_TurbineEventLog[] -> T_TurbineEventLog.
Thus, to me it seems that the few T_TurbineEventLog objects that I keep live myself for good reasons hold an indirect link to the totality of original SQL Query results through an event handler (keep in mind that the above shows the shortest root path).
I read the objects inside a small using (var db=new MyDataContext...)
, so I guess the datacontext ought to unsubscribe from the PropertyChanging event, but for some reason hasn't.
I have google'd to the best of my abilities but this doesn't seem like a known problem. So what can I be doing wrong?
The solution so far has been to set ObjectTrackingEnabled=false
, which actually removes the leak.
精彩评论