What is a Weak Reference?

Article Details
  • Written By: Rodney A. Crater
  • Edited By: Allegra J. Lingo
  • Last Modified Date: 20 October 2019
  • Copyright Protected:
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
People with auto-brewery syndrome convert carbs into ethanol in their gut, becoming drunk without drinking alcohol.  more...

November 13 ,  1956 :  The US Supreme Court upheld a decision that ended public bus segregation in Montgomery, Alabama.  more...

A weak reference is a reference to an object in memory that does not prevent an object from being cleaned up by garbage collection routines due to its association with the object. This type of reference is commonly used to help eliminate memory leaks when special conditions exist that prevent normal garbage collection from happening. Programming algorithms which use a circular reference or hold extraneous information in a cache or special structure benefit by using weak references. The utilization of weak references in these types of situations allows for the clean up of unused referent objects and their associated weak references.


In computer programming, a strong reference is typically created by default when an object is created, and the memory occupied by the object remains allocated as long as the strong reference remains viable. A strong reference becomes nullified and its associated object's memory becomes freed shortly after a strong reference and its object are flagged as no longer being used by a program. In certain situations, such as socket programming or image manipulation applications, the classes used to create working objects do not have the built-in capacity to store additional information which is necessitated by a specific application. The additional details for transient objects such as connections or pictures need to be stored in supplemental cache objects and linked to the transient objects by references. When a weak reference is used instead of strong reference, the extra information is not bound by the reference and may be removed from the cache by the garbage collector once the referent object has been marked as non-viable.

Weak reference classes are normally available in languages which are augmented by garbage collection subsystems, including Java, Python, Perl, C#, and the .NET framework. In C++, however, programmers are typically responsible for ensuring memory clean up. This language has implemented a smart pointer class which provides some of the same functionalities that weak references and garbage collection provide, helping to reduce memory leaks caused by improper pointer clean up.

To further facilitate memory management, languages with garbage collection facilities may additionally enhance the functionality of a language by providing finer gradients of references other than the strong or weak reference. Data types such as soft references and phantom references may be included in these languages, which allows for varying levels of object recapture or deallocation notification. Programmers who use languages with automated garbage collection should familiarize themselves with all of the reference classes that are available in order to improve application performance.


You might also Like


Discuss this Article

Post your comments

Post Anonymously


forgot password?