2

I have a very basic question regarding weak reference and strong reference in Java.

In general Java programming we generally do not create weak reference of object, we create normal strong reference but when we are done with that object we assign null to that object with the conception that, that object will be collected by GC next time.

Is that my understanding is wrong?

After reading some of the articles, it looks like, object is collected by GC If it is null or not referred anywhere if only it has weak reference. I am confused.

In other word what is the difference between these two code snippets, in respect to Java GC?

Snippet 1

Counter counter = new Counter(); // strong reference - line 1
WeakReference<Counter> weakCounter = new WeakReference<Counter>  (counter); //weak reference
counter = null;

Snippet 2

  Counter counter = new Counter(); // strong reference - line 1
  counter = null;
user207421
  • 305,947
  • 44
  • 307
  • 483
Sujoy
  • 117
  • 2
  • 9
  • The word you are looking for is *reference*. – user207421 Dec 05 '16 at 03:11
  • 1
    You may get some help reading the answer I posted to another question. WeakReference are not strong enough to force the object to survive a garbage collection cycle if there are no strong or soft references to it. https://stackoverflow.com/a/51891259/504133 – nits.kk Aug 21 '18 at 12:38

3 Answers3

2

In both cases, counter will be eligible for garbage collection. Even if you use SoftReference, it will be eligible for GC, but it will only be collected reluctantly. (That is, a SoftReference encourages the GC to leave the object in memory, but still allows it to be collected.)

Only hard references force the GC to leave objects alone.

Normally you only need to assign null to a reference if the reference has longer life than you want for the object. Once a hard-reference variable goes out of scope, it is no longer reachable from live code so its hard reference will not prevent the GC from collecting the object.

Note also that there's no guarantee as to when objects eligible for collection will actually be collected by the GC. It may be on the next GC cycle or maybe not. It depends heavily on the implementation of the GC. The only thing you can say for sure is that all eligible objects will be collected before the VM throws an OutOfMemoryError.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • is "hard-reference" the same, that "strong reference" - just a usual reference? – Ekaterina Jan 28 '22 at 15:29
  • 1
    @Ekaterina - Yes, they are the same thing. See, for example, [What is a hard reference in Java?](https://stackoverflow.com/q/6649580/535871). – Ted Hopp Jan 28 '22 at 16:49
0

The difference is that if I remeber correctly,

The First code snippit assigns the value null to the object counter after it has been assigned to weakCounter. So therfor weakCounter still has the reference to the old counter without the reference to counter being updated. But the counter is still collected by the compiler, even though the weakCounter is assigned to an object reference of Counter

In the second code example, the counter goes from being assigned to a object to a null value letting java know "hey you can collect me in the garbage!"

Hope this made sense and it helped your understanding if some of my facts are wrong please feel free to tell me where I am mistaken :)

beastlyCoder
  • 2,349
  • 4
  • 25
  • 52
  • Thanks for your reply ... do you mean one is eager process(weak reference) and other one is on request process (strong reference) – Sujoy Dec 05 '16 at 02:50
0

The two are essentially equivalent, save for the fact that you might be able to reference the object through the WeakReference if you do so before GC collects it.

The purpose of the WeakReference is so you can have it stashed somewhere (eg, some sort of search index) and not worry about having to clear it if you are done with the object and wish to null any "strong" references (so that the object may be collected and the space reused). If you used an ordinary strong reference you'd have to be sure to clear it or the object would hang around forever.

(SoftReferences, as mentioned by Ted Hopp, are similar in mechanics, except that GC will only collect the referenced objects if storage is tight. This makes them suitable for things like cached internet pages.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151