.NET 中是否存在弱引用?
我想在我的应用程序中保留某一类对象的列表。 但我仍然希望该对象被垃圾收集。 您可以在 .NET 中创建弱引用吗?
供参考:
来自 MSDN 的回答:
建立一个弱引用 对象,您创建一个 WeakReference 使用对象的实例 被跟踪。 然后您设置目标 属性到该对象并设置 对象为空。 对于代码示例, 请参阅类中的 WeakReference 图书馆。
I would like to keep a list of a certain class of objects in my application. But I still want the object to be garbage collected. Can you create weak references in .NET?
For reference:
- Garbage Collecting objects which keep track of their own instances in an internal Map
- Create a weak reference to an object
Answer From MSDN:
To establish a weak reference with an
object, you create a WeakReference
using the instance of the object to be
tracked. You then set the Target
property to that object and set the
object to null. For a code example,
see WeakReference in the class
library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是:
使用
System.WeakReference
。
Yes:
Uses
System.WeakReference
.是的,有一个通用的弱引用类。
MSDN > 弱引用
Yes, there's a generic weak reference class.
MSDN > Weak Reference
是的...
这里有一个很好的例子:
http://web.archive.org/web/20080212232542/http://www.robherbst.com/blog/2006/08/21/c- weakreference-example/
在你的类中,你创建了两个成员变量:
你创建了两个新的
Person
对象(它们是我刚刚为本示例创建的简单对象,由Name< /code> 属性和一些参考跟踪代码)。 接下来,将成员变量设置为新创建的
Person
对象实例。这里的区别是,
_strongRef
只是一个常规的普通引用,而_weakRef
被设置为带有 person 对象的WeakReference
对象< code>(p1) 作为构造函数中的参数传入。如果要发生垃圾收集,或者只是为了测试目的,您自己调用它:
那么由
_weakRef
成员变量保存的p1
目标对象应该被垃圾收集。 您可以编写代码来检查:如果
WeakReference
仍然存在,您可以使用如下代码将WeakReference
转换为强引用或普通引用:现在
p
引用被视为强引用,在不再使用之前不会被收集。 如果您想在范围之后保留引用,您可以将其设置为成员变量。Yes...
There is a pretty good example to be found here:
http://web.archive.org/web/20080212232542/http://www.robherbst.com/blog/2006/08/21/c-weakreference-example/
In your class you created two member variables:
You created two new
Person
objects (which are simple objects I just created for this example, consisting of aName
property and some reference tracking code). Next you set the member variables to the newly created instances of thePerson
objects.The difference here you’ll notice that
_strongRef
is just a regular normal reference, whereas_weakRef
is set to aWeakReference
object with the person object(p1)
passed in as a parameter in the constructor.If a garbage collection were to occur, or just for testing purposes you called it yourself with:
Then the
p1
target object that is held by the_weakRef
member variable should be garbage collected. You can write code to check:If the
WeakReference
is still alive you can convert theWeakReference
to a strong or normal reference by using code like this:Now the
p
reference is treated as a strong reference and won’t be collected until it is no longer used. If you wanted to keep the reference around after the scope you could set that to a member variable.这是
WeakReference
的完整(非线程安全)实现示例WeakReference
位于System
命名空间中,因此无需为其包含任何特殊程序集。Here is the full (non thread safe) implementation sample of
WeakReference
WeakReference
is in theSystem
namespace hence no need to include any special assembly for it.