• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

when to use weak references

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the use of using weak references? what will we achieve using it?
In what situations using it will be helpful?
Please explain with the help of example
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

anish jain wrote:What is the use of using weak references? what will we achieve using it?
In what situations using it will be helpful?
Please explain with the help of example



A weak reference is good for cases where a piece of data is only useful if it is actually used. And the holder of the data is not a candidate to use it.


For example, the ThreadLocal class needs a reference to the Thread classes to store local data.... but... if the Thread object isn't used by any other threads, it is merely holding data that is no longer needed. And because it is holding the data, the thread instances won't be garbage collected, even though no other threads needs it.

To solve this, the ThreadLocal class uses a weak reference to the thread object. When all other references are gone, the data will be GC'ed from the threadlocal instance, even if the threadlocal instance is still reachable.

Henry
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recommend perusing the results of a search for java weak reference tutorial..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic