开发者

When my timer ticks.... .NET Memory Leak

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

On each tick 开发者_如何学编程of the timer, the code allocates an IDisposable object (called SocketsMessageConnector)...but I do dispose it correctly.

I ran .NET Memory Profiler and every 60 seconds I see a new instance of my SocketsMessageConnector class lingering in memory (so after 15 minutes, I have 15 instances). The memory profiler verifies that the instance is in fact disposed, but it shows the instance rooted by a TimerCallback, which is rooted by a _TimerCallback, which is rooted by a GCHandle...

What's up here? Why is the TimerCallback holding on to the new instance created on every timer tick?

PS. The profiler forces 2 GCs before taking a snapshot, so I know it IS in fact a leak and not just an optimization by the GC.


Just because it's been disposed, doesn't mean that it's been Garbage Collected yet.

Try changing your timer to run twice a second, and then let it run for 10 minutes. Now check how many of your class objects are still "lingering in memory". If you truely have a memory leak, you'll have 1200 objects. But if Garbage Collection has jumped in, you'll have considerably less - perhaps under 100.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜