Memory Leak in managed Code

  • Thread starter Thread starter Guest
  • Start date Start date
Jon Skeet said:
It sort of is, actually. The finalizer thread is getting blocked, so
none of the test class instances are getting collected. I believe the
finalizer thread is waiting to execute code on the STAThread, which it
can't do because that thread is always busy.

It's worth noting that any of the following remove the problem:

1) Running under .NET 2.0
2) Removing the creation/disposal of the connection
3) Removing the STAThread attribute
4) Removing the finalizer from TestClass

Jon,

Could add a call To WaitForPendingFinalizers() after the Dispose call, with
the STAThread set and see what happens?
I'm looking at the v1.1 sources and found a COM dependency, this part is
somewhat complex code so before I go deeper I would see this confirmed.
Why? Well, it would confirm that the finalizer is blocked trying to finalize
the RCW on an STA thread that doesn't pump messages! (which is real bad).
That would also explain why there is no issue on an MTA thread.

Willy.
 
Ignacio Machin ( .NET/ C# MVP ) said:
Hi Willy,


I tested this with a remote server and the same thing happened.

Ignacio,

You mean it's leaking, right? Weird, I run this on v1.1.4322 SP1 XP SP2 with
SQL2005 as backend on W2K3.
I'm also using SSPI as security protocol. I will double check to see if I'm
not mixing v1.1 and v2 stuff.

Willy.
 
Ignacio Machin ( .NET/ C# MVP ) said:
Hi Willy,


I tested this with a remote server and the same thing happened.

Ok, guys I'm also experiencing the leak on 1.1.4233, must have made a
mistake when compiling. Sorry about the confusions.

Willy.
 
Jon Skeet said:
It sort of is, actually. The finalizer thread is getting blocked, so
none of the test class instances are getting collected. I believe the
finalizer thread is waiting to execute code on the STAThread, which it
can't do because that thread is always busy.

It's worth noting that any of the following remove the problem:

1) Running under .NET 2.0
2) Removing the creation/disposal of the connection
3) Removing the STAThread attribute
4) Removing the finalizer from TestClass

Ok, no need to check any further.
The 'leak' is due to a blocked finalizer thread because of a COM dependency
is the SqlClient namespace.
What you should do is call GC.WaitForPendingFinalizers() in order to pump
the message queue in an STA thread. Console applications don't pump messages
(actually they have no message queue), that's why I insist you should never
apply STAThread in a console (or console like) application.
This is an issue I posted several times about before, and the rule is
simple, if you really needs an STA thread for a console application you
should must pump messages.
To help you with this the framework provides API's like
WaitForPendingFinalizers(), WaitOne() and a couple of others that do pump
the queue (for COM messages only).
But again, if you don't need an STA thread, stay away from it, they are only
meant for Windows Forms applications UI threads.


Willy.
 
Back
Top