0

Iam a little bit confused about what does WeakReference actually do. From what i've read let me explain:

Example1:

Integer prime = 1;  
WeakReference<Integer> soft = new WeakReference<Integer>(prime); 
prime = null;

In the next gc cycle prime will be collected. So far so good. But if i do this without WeakReference:

Integer prime = 1;  
prime = null;

Isn't the same thing? Prime will be collected again because there is no reference.

Nick
  • 2,818
  • 5
  • 42
  • 60
  • 4
    Even setting to `null` is unnecessary, as `prime` will probably fall out of scope and be eligible for GC. Does this mean `WeakReference` is useless? No, it just means your example is very poor. I recommend searching existing questions on `WeakReference`, but you probably shouldn't worry too much about it. It's a very specialized class, not something you use regularly. Also if you're explicitly setting references to `null` as if by default, that's a bad and useless habit. – Kayaman May 23 '18 at 10:35
  • In your first example you could however write `soft.get()` in the next line while you could not do that in your second example. I mean the whole idea of a WeakReference is not to enable garbage collection but to have a weak reference to an object that will be GCed when there is no strong reference to it anymore. There are use cases where a WeakReference is what you want. – Ben May 23 '18 at 10:35
  • @Kayaman why didn't you write it as an answer? – Imaskar May 23 '18 at 10:37
  • 1
    @Imaskar I should've looked for a dupe instead. – Kayaman May 23 '18 at 10:39
  • @Kayaman What do you mean by "will probably fall out of scope"? – Nick May 23 '18 at 10:39
  • Related: https://stackoverflow.com/questions/7136620/weak-references-how-useful-are-they – Ben May 23 '18 at 10:39
  • Well a variable falling out of scope (or rather the object it's referring to) will be eligible for GC whether you've assigned `null` to it or not. As short lived objects are very common compared to long lived objects, you rarely see explicit nulling of variables. Not that it matters, the example itself is flawed. – Kayaman May 23 '18 at 10:44
  • @Kayaman even falling out of scope is not necessary if the object is otherwise unused. See [finalize() called on strongly reachable object in Java 8](https://stackoverflow.com/questions/26642153/finalize-called-on-strongly-reachable-object-in-java-8)… – Holger May 25 '18 at 14:10
  • @Holger interesting, thanks for the link. – Kayaman May 25 '18 at 16:34

1 Answers1

2

Your example is not a particularly good one. It's certainly not a valid use-case for a WeakReference. For starters, the Integer 1 will always be present in the Integer cache, so will always have a strong reference to it.

A WeakReference will hold a reference to an object as long as something else holds a (strong) reference to it. Imagine a particularly fickle person. They will like a TV show/song/movie ... but only if someone else likes it. Once everyone else stops liking that thing, they will promptly lose interest as well.


Also, be careful here:

WeakReference<Integer> soft

WeakReference and SoftReference are closely related but are not interchangeable.

Michael
  • 41,989
  • 11
  • 82
  • 128
  • Is it useless to setting variables to null? If that so, how else do we declare that we want an object to be garbage collected? – Nick May 23 '18 at 10:44
  • Local variables? Yes, most of the time it's useless. They will shortly go [out of scope](https://docs.oracle.com/javase/tutorial/jndi/ops/scope.html) and will be eligible for garbage collection anyway. Fields of classes are a slightly different matter. Sometimes there are valid reasons to make those null. – Michael May 23 '18 at 10:45
  • @Michael for optimized code, it is not even necessary that local variables go out of scope. All that matters, is, whether the object is subsequently used. For non-optimized executions, references may sometimes hanging in a stack frame, even if the variable is out of scope. Usually, it doesn’t matter. Only in very rare corner cases, setting a variable to `null` may have a point (which would require understanding of the underlying byte code and a particular JVM’s garbage collection algorithm). – Holger May 25 '18 at 14:14