不要静态成员曾经被回收? [英] Do static members ever get garbage collected?

查看:179
本文介绍了不要静态成员曾经被回收?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不要静态成员变量曾经被回收?

Do static member variables ever get garbage collected?

例如,让我们用下面的类。

For example, let's use the following class.

public class HasStatic {
    private static List<string> shared = new List<string>();

}

和假设它的使用是这样的:

And supposed that it's used like this:

//Startup
{
HasStatic a = new HasStatic();
HasStatic b = new HasStatic();
HasStatic c = new HasStatic();
HasStatic d = new HasStatic();
//Something
}
//Other code
//Things deep GC somewhere in here
HasStatic e = new HasStatic();

A B C ,和 D 的垃圾回收不静态成员共享得到收集呢?莫非电子可能获得的新实例共享

When a, b, c, and d are garbage collected does the static member shared get collected as well? Could e possibly get a new instance of shared?

推荐答案

没有,静态成员与类型,这与它在加载的AppDomain中相关的关联。

No, static members are associated with the Type, which is associated with the AppDomain it's loaded in.

请注意,有没有必须的任何的该类 HasStatic 的实例初始化和共享变量有一个参考列表与LT;字符串方式&gt;

Note that there doesn't have to be any instances of HasStatic for the class to be initialized and the shared variable to have a reference to a List<string>.

除非你正在考虑在那里的AppDomain得到空载情况下,静态变量可以永远视为GC根。 (当然,如果有新的变化值 HasStatic.shared 引用不同的实例,一审的可能的符合垃圾收集。)

Unless you're considering situations where AppDomains get unloaded, static variables can be regarded as GC roots forever. (Of course, if something changes the value of HasStatic.shared to reference a different instance, the first instance may become eligible for garbage collection.)

这篇关于不要静态成员曾经被回收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆