Do static members ever get garbage collected?

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

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

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

Leave a Comment