There is a memory leak in a legacy application, so I don’t know the code very well. I analyzed with MemProfiler and found out that a growing number of objects is referenced by TimerCall objects. These TimerCallbacks are referenced by _TimerCallback objects and these are children of the root. The graph looks like Root -> _TimerCallback -> TimerCallback -> MyObject.
Since MyObject needs a TimerCallback in its constructor there are several calls like:
new MyObject(int timeout, new TimerCallback(SomeCallbackMethod));
To be honest it looks quite awkward to me to do it like this, but as I wrote it’s a legacy application and it was written in “C style” C#, with lots of null assignments and Dispose() calls …
So how can I remove these references to TimerCallback from root?
This looks like your
TimerCallbackis being used in aTimerobject. Find out, why there are a lot of Timers active. They are likely to be the real roots keeping your callback alive.I was just looking around the
_TimerCallbackclass in Reflector and although I don’t fully comprehend what’s going on I think this class is a red herring. I think the root cause are Timers (maybe even disabled ones).