When my timer is ticking ..... NET Memory Leak

I have a .NET System.Threading.Timer timer that ticks every 60 seconds and introduces a memory leak on each tick.

At each tick of the timer, the code allocates an IDisposable (called SocketsMessageConnector) ... but I am disposing correctly.

I started the .NET Memory Profiler and every 60 seconds I see a new instance of the SocketsMessageConnector class that lingers in memory (so after 15 minutes I have 15 instances). The memory profiler checks to see if the instance is actually hosted, but it shows the instance embedded in TimerCallback, which is embedded using _TimerCallback, which is embedded in GCHandle ...

What is here? Why is TimerCallback holding onto a new instance created with every timer?

PS. The profiler forces 2 GCs to the snapshot, so I know this is really a leak, not just a GC optimization.

+7
source share
1 answer

Just because it was deleted does not mean that it was a garbage collection.

Try changing your timer to run twice per second, and then let it run for 10 minutes. Now check how many of your class objects are still "lingering in memory." If you really have a memory leak, you will have 1200 objects. But if the garbage collection jumped up, you will have significantly less - maybe up to 100.

+7
source

All Articles