其实C#弱引用软?

2023-09-03 10:55:04 作者:- 旧梦不需记

的基本区别是,弱引用都应该被要求在GC上的每次运行(保持内存占用低),而软引用应该被保存在内存中,直到GC实际上需要的内存(他们试图扩大寿命,但可能随时失败,这是对于例如高速缓存特别的相当昂贵的对象)是有用的。

The basic difference is that weak references are supposed to be claimed on each run of the GC (keep memory footprint low) while soft references ought to be kept in memory until the GC actually requires memory (they try to expand lifetime but may fail anytime, which is useful for e.g. caches especially of rather expensive objects).

据我所知,目前还没有明确说法,以弱引用如何影响在.NET中的对象的生命周期。如果他们是真正的弱者裁判,他们不应该影响它在所有的,但是,这也将使其pretty的没用的,我相信,缓存的主要目的(我错了吗?)。在另一方面,如果他们像柔软的裁判,他们的名字是有点误导。

To my knowledge, there is no clear statement as to how weak references influence the lifetime of an object in .NET. If they are true weak refs they should not influence it at all, but that would also render them pretty useless for their, I believe, main purpose of caching (am I wrong there?). On the other hand, if they act like soft refs, their name is a little misleading.

就个人而言,我想他们表现得像软引用,但是这仅仅是一个IM pression不成立。

Personally, I imagine them to behave like soft references, but that is just an impression and not founded.

细则申请,当然。我问有关与.NET的弱引用相关联的心态 - 他们能够扩大一辈子,还是他们表现得像真正的弱裁判?

Implementation details apply, of course. I'm asking about the mentality associated with .NET's weak references - are they able to expand lifetime, or do they behave like true weak refs?

(尽管有一些相关的问题,我无法找到一个答案,这个具体问题呢。)

(Despite a number of related questions I could not find an answer to this specific issue yet.)

推荐答案

我没有看到任何信息,表示他们将增加它们指向的对象的生命周期。而文章中,我读到了GC用来确定可达性没有提到他们以这种方式无论是算法。所以,我希望他们对对象的生命周期没有影响。

I have seen no information that indicates that they would increase the lifetime of the object they point to. And the articles I read about the algorithm the GC uses to determine reachability do not mention them in this way either. So I expect them to have no influence on the lifetime of the object.

弱的   该句柄类型用于跟踪对象,但允许它被收集。当一个对象的收集,所述的GCHandle的内容被置零。弱引用归零终结器运行之前,因此即使终结复活对象,弱引用仍为零。

Weak This handle type is used to track an object, but allow it to be collected. When an object is collected, the contents of the GCHandle are zeroed. Weak references are zeroed before the finalizer runs, so even if the finalizer resurrects the object, the Weak reference is still zeroed.

WeakTrackResurrection 的   此句柄类型类似于弱,但手柄不为零,如果该对象被终结过程中复活了。

WeakTrackResurrection This handle type is similar to Weak, but the handle is not zeroed if the object is resurrected during finalization.

http://msdn.microsoft.com/en-us/library/83y4ak54.aspx

有一些机制,通过它的对象是不可达可以生存的垃圾收集。

There are a few mechanism by which an object that's unreachable can survive a garbage collection.

的对象的代是比发生了GC的产生变大。这对于大的物体,被分配在大对象堆和总被认为是第二代为此,特别有趣。 有一个终结的对象和所有生存的GC对象,从他们到达。 有可能是一种机制,从老物件前引用可以保持年轻的对象还活着,但我不知道这一点。
相关推荐