静态成员的垃圾收集

1

本文介绍了静态成员的垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

静态成员会被垃圾收集器收集吗?

Will static members be ever collected by the garbage collector?

推荐答案

静态变量引用的对象只有在相关的AppDomain被垃圾回收时才会被垃圾回收.在客户端应用程序中,通常只有一个 AppDomain 在整个过程中存在.(例外情况是应用程序使用插件架构 - 不同的插件可能会加载到不同的 AppDomain 中,而 AppDomain 可能会在以后卸载.)

Objects referenced by static variables will only be garbage collected when the relevant AppDomain is garbage collected. In client applications, there's often just a single AppDomain which lives for the duration of the process. (An exception is when the application uses a plug-in architecture - different plug-ins may be loaded in different AppDomains and the AppDomain may be unloaded later.)

在 ASP.NET 中,AppDomain 回收"会定期发生(出于各种原因) - 当这种情况发生时,该 AppDomain 中的静态变量将不再充当GC 根,因此不会阻止对象被垃圾回收.

In ASP.NET, "AppDomain recycling" happens periodically (for various reasons) - when this occurs, and the static variables within that AppDomain will no longer act as GC roots, and thus won't prevent objects being garbage collected.

如果您担心一个对象被垃圾回收,而您仍然通过静态变量引用它,那么您可以放心.虽然您可以访问该对象,但它不会被垃圾回收.

If you were worried about an object being garbage collected while you still had a reference to it via a static variable, though, you can relax. While you can access the object, it won't be garbage collected.

这篇关于静态成员的垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14

函数委托与函数
Func Delegate vs Function(函数委托与函数)...
2023-11-11 C#/.NET开发问题
6

具有未知类型的 CreateDelegate
CreateDelegate with unknown types(具有未知类型的 CreateDelegate)...
2023-11-11 C#/.NET开发问题
5