有关非静态类和垃圾回收中的静态成员的问题 [英] 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?
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.
这篇关于有关非静态类和垃圾回收中的静态成员的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!