views:

91

answers:

3

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?

+5  A: 

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.

Joel Coehoorn
A: 

You friend is incorrect.

The idea of a static method is that there is NO instance of that class. So nothing exists to garbage collect.

Try putting this inside of a static method in a non-static class and see what happens.

Alastair Pitts
+1  A: 

Static members are roots for the GC. Anything referenced from a static will be kept alive. Whether the static reference is in a static class or in a non-static class is irrelevant.

If you have a non-static class which has a static field, and you have instances of that class, the static field doesn't have many instances - that's part of the definition of static - it's not a per instance field. So whether the class itself is static or not makes no difference.

So yes, static references are often a cause of memory leaks, especially static events that you have not unsubscribed from when it's appropriate. Changing the class to be static will not solve your memory leak - you need to remove the static reference when the lifetime of the instance it's referring to is finished. Often this is done through Dispose()ing the object and having the Dispose clean up the reference/event subscription.

This is a good place to learn more about how the GC works, how it identifies garbage and what it does about it. As well as Finalisers and more...

Niall Connaughton