Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A question about 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?

like image 465
MrLane Avatar asked Oct 08 '10 03:10

MrLane


3 Answers

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.

like image 200
Joel Coehoorn Avatar answered Sep 25 '22 11:09

Joel Coehoorn


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.

like image 35
Alastair Pitts Avatar answered Sep 24 '22 11:09

Alastair Pitts


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...

like image 28
Niall Connaughton Avatar answered Sep 22 '22 11:09

Niall Connaughton