How? static members inside non-static classes and garbage collection

A collegue of mine claims that in C# having static members in non-static classes prevents instances of those classes from ever being garbage collected and that this is a common source of C# memory leaks. As a result he always wraps static members in a static class and gains access to them from there via a static property or method(s) on that static class. I always thought that statics were on the stack, not the heap and so did not have anything to do with the garbage collection. It does not seem right to me.
What is the truth about this?

Answer is

He doesn't know what he's talking about. Static members inside a non-static class do not prevent instances of the class from being garbage collected.
That said, statics can be on the stack or heap. It doesn't matter for garbage collection. What does matter is that the static parts of a type are not stored with instances of the type.

0 comments: