Will static members be ever collected by the garbage collector?
Members are not collected... Objects are.
So if you set the Ref. Type static member to null, any object it was previously pointing to would be collected. If not, it will hang around till the AppDomain goes down (Each AppDomain has its own set of static stuff)
Objects referenced by static variables will only be garbage collected when the relevant AppDomain
is garbage collected. In client applications, there's often just a single AppDomain
which lives for the duration of the process. (An exception is when the application uses a plug-in architecture - different plug-ins may be loaded in different AppDomain
s and the AppDomain
may be unloaded later.)
In ASP.NET, "AppDomain
recycling" happens periodically (for various reasons) - when this occurs, and the static variables within that AppDomain
will no longer act as GC roots, and thus won't prevent objects being garbage collected.
If you were worried about an object being garbage collected while you still had a reference to it via a static variable, though, you can relax. While you can access the object, it won't be garbage collected.
A static member to a reference type is a reference, which may or may not point to an instance. If it points to an instance, said instance will not be collected until the static member is unloaded. If the type is loaded in a specific AppDomain, that can be unloaded. Otherwise it only happens when the application is terminated.