Jump to content

Soft reference

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by PetrGlad (talk | contribs) at 05:51, 24 June 2014. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A soft reference is one of the strengths or levels of 'non strong' reference defined in the Java programming language, the others being weak and phantom. In order from strongest to weakest, they are: strong, soft, weak, phantom.

Soft references behave almost identically to weak references, but they are garbage-collected less aggressively. Soft and weak references provide two quasi-priorities for non-strongly referenced objects: the garbage collector will always collect weakly referenced objects, but will only collect softly referenced objects when its algorithms decide that memory is low enough to warrant it.

Soft references may be used, for example, to write a free memory sensitive cache such that cached objects are kept until there is enough heap space. In some cases weakly referenced objects may be reclaimed too quickly to make such cache useful.

See also

  • Java developer article: 'Reference Objects and Garbage Collection'
  • Nicholas, Ethan (May 4, 2006). "Understanding Weak References". java.net. Retrieved October 1, 2010. {{cite web}}: External link in |publisher= (help)