弱引用的好处 [英] Weak reference benefits

查看:174
本文介绍了弱引用的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能在C#中解释不同类型的引用的主要好处是什么?

Can someone explain the main benefits of different types of references in C#?


  • 弱引用

  • 软引用

  • 虚引用

  • 强引用。

我们有一个消耗大量的内存和我们正试图确定这是否是专注于一个领域的应用。

We have an application that is consuming a lot of memory and we are trying to determine if this is an area to focus on.

推荐答案

柔软虚引用来自Java的,我相信。长弱引用(true传递给了C#构造函数的WeakReference)可能被视为类似于Java的的PhantomReference。如果有一个模拟在C#中SoftReference的,我不知道它是什么。

Soft and phantom references come from Java, I believe. A long weak reference (pass true to C#'s WeakReference constructor) might be considered similar to Java's PhantomReference. If there is an analog to SoftReference in C#, I don't know what it is.

弱引用不延长对象的生命周期,从而使其能够被垃圾收集一旦所有强引用都超出范围。他们可以为持有至价格昂贵初始化大对象是有用的,但如果他们不积极使用时应进行垃圾回收。

Weak references do not extend the lifespan of an object, thus allowing it to be garbage collected once all strong references have gone out of scope. They can be useful for holding on to large objects that are expensive to initialize, but should be available for garbage collection if they are not actively in use.

这是否会降低应用程序的内存消耗将完全取决于应用的具体有用的。例如,如果你有一个中等数量的缓存的对象挂在了5月的会或可能不会在将来被重用,弱引用可以帮助提高缓存的内存消耗。但是,如果应用程序正在与一个非常大的一些小物体,弱引用会使问题变得更糟,因为引用对象将采取尽可能多或更多的内存。

Whether or not this will be useful in reducing the memory consumption of your application will depend completely on the specifics of the application. For example, if you have a moderate number of cached objects hanging around that may or may not be reused in the future, weak references could help improve the memory consumption of the caches. However, if the app is working with a very large number of small objects, weak references will make the problem worse since the reference objects will take up as much or more memory.

这篇关于弱引用的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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