• 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

WeakReferences in Java

 
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused about the Weak References in Java. It is said that it will be garbage collected if we make it as null but I think the same is true for Strong References as well. Garbage Collector will identify all the null references for Garbage Collection.

So, what is the basic difference in Weak and Strong References? I am totally confused
 
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
First, a "strong reference" is simply a reference to an object. If you can access the reference, then the object is reachable, and the object can't be garbage collected.

A WeakReference instance is just an object that points (references) another object. If you instantiate a WeakReference object, and have it hold a reference to the target object, then there is a weak reference to the target object. Of course, to use it, you have to use the WeakReference object, to get back the reference to the target object, so that you may use it.

Now, why is it a weak reference? WeakReference instances have a special property when it comes to the Garbage Collector. If the GC detects that the only references that are accessing an instance are via WeakReference instances, then it is considered not reachable -- and the target object will be garbage collected. Of course, when this happens, if you use the WeakReference instance to obtain the target object, you will get a null result.

Henry
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few years ago I wrote a detailed post about weak, soft and phantom references. Here is the link: https://coderanch.com/t/555619/java/java/Phantom-Reference

I hope it's useful.
 
Vaibhav Gargs
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry and Stephen.

As per link, http://javarevisited.blogspot.in/2014/03/difference-between-weakreference-vs-softreference-phantom-strong-reference-java.html

[code=java]
Counter counter = new Counter(); // strong reference - line 1
WeakReference<Counter> weakCounter = new WeakReference<Counter>(counter); //weak reference
counter = null; // now Counter object is eligible for garbage collection

[/code=java]

Here, when we set counter as null, so, won't it be eligible for GC even if it is a Strong Reference? And, why one should go for a Weak Reference as they can be GC'ed during execution of the program even if we may need them, so, what is the rationale behind using weak references when we don't get the object while we need it?
 
Henry Wong
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

Vaibhav Gargs wrote:
Here, when we set counter as null, so, won't it be eligible for GC even if it is a Strong Reference?



Being eligible of GC, and being actually GC'ed are not the same thing. With a weak reference, and if GC hasn't occurred yet, a strong reference can be obtained again via the use of the get() method.

Henry
 
Henry Wong
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

Vaibhav Gargs wrote: And, why one should go for a Weak Reference as they can be GC'ed during execution of the program even if we may need them, so, what is the rationale behind using weak references when we don't get the object while we need it?



Your rationale argument is kinda a bit of a straw man argument ...

I would ask the reverse. Why would you care to keep strong references to stuff that you no longer care about?  A classic example is via the use of the WeakHashMap class. You only care to keep stuff in the hashmap that is needed by other parts of your program -- and you don't want the hash map itself to prevent GC, because it maintains a strong reference to stuff that is no longer needed by other parts of your program.

Henry
 
Vaibhav Gargs
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry!!

Frankly speaking, I am totally confused and not getting the concept of Weak References Not sure if this concept is really that hard to understand or am i too dumb :'(
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are four kinds of such references: this and its three subclasses. If the only reference to an object is via a Reference object, and certain other conditions apply, that object can be reachable and also eligible for garbage collection.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic