Weak Reference Article Index for
Weak
Website Links For
Reference
 

Information About

Weak Reference





GARBAGE COLLECTION

. Weak references may be used to solve the problem of circular references if the reference cycles are avoided by using weak references for some of the references within the group.

Weak references are also used to minimize the number of unnecessary objects in memory by allowing the program to indicate which objects are not critical by only weakly referencing them.


VARIOUS IMPLEMENTATIONS

Some languages have multiple levels of weak reference strength. For example, Java has — in order of decreasing strength — Soft , weak, and Phantom references, defined in the Package Java.lang.ref .

Some non-garbage-collected languages, such as C++ , provide weak/strong reference functionality as part of supporting garbage collection libraries. In the case of C++, normal pointers are "weak" and Smart Pointer s are "strong" (although pointers are not ''true'' weak references, as weak references are supposed to know when the object becomes unreachable).


EXAMPLES

Weak references can be useful in keeping track of the current variables being referenced in the application. This list must have weak links to the objects. Otherwise, once objects are added to the list, they will be referenced by it and will persist forever (or until the program stops).

Another use of weak references is in writing a Cache . Using, for example, a weak Hash Map , one can store in the cache the various referred objects via a weak reference. When the garbage collector runs — when for example the application's memory usage gets sufficiently high — those cached objects which are no longer directly referenced by other objects are removed from the cache.


EXTERNAL LINKS