因为垃圾回收器是一个优先级很低的线程,所以弱引用对象也不一定会马上就会被回收。
GC is a thread of very low priority, so a weak reference object may not be collected immediately.
一个弱引用对象将会保持一个指向一个超出作用域或者被置空的对象的引用,直到它确实被垃圾回收删除。
A weak reference object will keep a reference to an object that went out of scope or was set to null, until it is actually deleted by the garbage collector.
这些图持有弱引用,允许连接这样的被跟踪对象在只有方面引用它们的时候,被垃圾收集掉。
These maps hold weak references to allow tracked objects like connections to be garbage collected when they are referenced only by the aspect.
在前一期文章中我们看到了,弱引用如何可以给应用程序提供当对象被程序使用时另一种到达该对象的方法,但是不会延长对象的生命周期。
In the previous installment, we saw how weak references can provide an application with an alternate means of reaching an object while it is used by the program, but without prolonging its lifetime.
如果已经分配了最低数量的对象,则对象的弱引用被放置到队列中。
If the minimum number of objects is already allocated, then a weak reference to the object is enqueued.
在使用这个if块之后的obj之后,您应该将ob j设置为null,从而让垃圾收集器能够回收这个对象以充分利用弱引用。
After working with obj after this if block, you should set obj to null to enable the garbage collector to reclaim this object to take full advantage of weak references.
WeakHashMap的实现展示了弱引用的一种常见用法——一些内部对象扩展WeakReference。
The implementation of WeakHashMap illustrates a common idiom with weak references — that some internal object extends WeakReference.
弱引用告诉垃圾收集器如果某个对象除了弱引用之外没有其他任何引用,则应该收集该对象。
Weak references tell the garbage collector that it should collect an object if there are no references to it other than the weak reference.
处理软引用之后,弱可及对象的集合被识别——这样的对象上不存在强引用或软引用。
After processing the soft references, the set of weakly reachable objects is identified — objects for which no strong or soft references exist.
哪怕及时释放掉了实际的对象,但WPF的弱引用数组还是会继续持有引用,这就导致了内存泄漏。
Even when the actual objects are being released in a timely manner, WPF's arrays of weak references hang around and under stress become the memory leak.
与弱引用一样,软引用通过利用垃圾收集器在作出缓存回收决策方面的帮助,有助于防止应用程序出现对象游离。
Soft references, like weak references, can help applications prevent object loitering by enlisting the aid of the garbage collector in making cache eviction decisions.
在短周期对象销毁后,长周期对象对短周期对象的弱引用依然会长久保留下来。
The weak reference from the long-lived object to the short-lived object is kept long after the short-lived object has been destroyed.
弱可及对象(weaklyreachable):不是强可及对象也不是软可及对象,并且能够通过弱引用访问的对象。
Weakly reachable: an object that is not strongly or softly reachable and can be accessed through a weak reference.
虚可及对象(phantomly reachable):不是强可及对象、软可及对象,也不是弱可及对象,已经结束的,可以通过虚引用访问的对象。
Phantomly reachable: an object that is not strongly, softly, or weakly reachable, has been finalized, and can be accessed through a phantom reference.
另一方面,在创建对一个对象的弱引用时,完全没有扩展referent的生命周期,只是在对象仍然存活的时候,保持另一种到达它的方法。
On the other hand, when you create a weak reference to an object, you do not extend the lifetime of the referent at all; you simply maintain an alternate way to reach it while it is still alive.
而且,内存中的一个对象可以被多个引用(可以是强引用、软引用、弱引用或虚引用)引用。
Furthermore, an object in memory can be referenced by multiple references that could be strong, soft, weak, or phantom. Before proceeding further, let's look at some terminology.
弱引用是对一个对象(称为referent)的引用的持有者。
A weak reference is a holder for a reference to an object, called the referent.
关键之处在于,垃圾收集器运行时如果碰到了弱可及对象,将释放WeakReference引用的对象。
The key point is that when the garbage collector runs, if it encounters a weakly reachable object, it will free the object the WeakReference refers to.
从以上是否能看出,一个对象不能同时具有软引用和弱引用?
From above shall we say that an object can't have a soft reference and a weak reference at the same time?
仅在必要时使用长弱引用,因为在终止后对象的状态是不可预知的。
Use long weak references only when necessary as the state of the object is unpredictable after finalization.
弱引用特别适合以下对象:占用大量内存,但通过垃圾回收功能回收以后很容易重新创建。
Weak references are useful for objects that use a lot of memory, but can be recreated easily if they are reclaimed by garbage collection.
一点弱引用是指一个对象不强迫它留在记忆里。
The point of a weak reference is to refer to an object without forcing it to stay in memory.
弱引用允许应用程序访问对象,同时也允许垃圾回收器收集相应的对象。
A weak reference permits the garbage collector to collect the object while still allowing the application to access the object.
如果一个指针指向一个不在图中的对象,那么此对象就是一个不可到达的对象,垃圾收集器就将短弱引用表相应的槽置空。
If a pointer pointing to an object not in the graph, then the object is not a reachable object, and then garbage collector sets the slot to null in the short weak reference table.
如果一个指针指向一个不在图中的对象(注意此时图中已包含Finalize可到达的对象),那么此对象就是一个不可到达的对象,垃圾收集器就将长弱引用表相应的槽置空。
If a pointer pointing to an object not in the graph, then the object is not a reachable object, and then garbage collector sets the slot to null in the long weak reference table.
如果一个指针指向一个不在图中的对象(注意此时图中已包含Finalize可到达的对象),那么此对象就是一个不可到达的对象,垃圾收集器就将长弱引用表相应的槽置空。
If a pointer pointing to an object not in the graph, then the object is not a reachable object, and then garbage collector sets the slot to null in the long weak reference table.
应用推荐