5

What is the difference between using WeakReference and setting the strong ref type to null ?

Say for example in the following code the variable "test" is a strong ref to "testString". When I set the "test" to null. There is no longer a Strong ref and hence "testString" is now eligible for GC. So if I can simply set the object reference "test" to equal null what is the point of having a WeakReference Type ?

class CacheTest {
  private String test = "testString";

  public void evictCache(){
    test = null; // there is no longer a Strong reference to "testString" 
    System.gc(); //suggestion to JVM to trigger GC 
  }
}

Why would i ever want to use WeakReference ?

class CacheTest {
  private String test = "testString";
  private WeakReference<String> cache = new WeakReference<String>(test);

  public void evictCache(){
    test = null; // there is no longer a Strong reference to "testString" 
    System.gc(); //suggestion to JVM to trigger GC 
  }
}
Assafs
  • 3,257
  • 4
  • 26
  • 39
Shivam Sinha
  • 4,924
  • 7
  • 43
  • 65
  • disagree the scope of the question you referenced is very board, here i am asking a very explicit question with an example. – Shivam Sinha Sep 27 '17 at 16:34
  • @ShivamSinha the dupe answer and the docs cover this in detail. You don't need weak references when you don't mind just nulling out the reference. The point of the set of weaker references is that you don't have to do that and you let the GC handle it for you. Consider it analogous to 'what's the point of GC, I can just free stuff when I no longer need it' – pvg Sep 27 '17 at 16:36
  • 3
    For this simple scenario WeakReference's are overkill. If you have a larger application where resources are shared among many other objects it gets more fuzzy who has still a strong reference on the resource. Here you usually have a central point, some kind of registry, that holds the only strong reference to the resource and only passes weak references to clients. That way it is guaranteed that the resource may become eligible for GC if the resource manager/registry is removing it else some other object might still have a strong reference kept in some list, map, whatever. – Roman Vottner Sep 27 '17 at 16:38
  • @pvg thanks that answers my question. However I would still argue that "dup" question doest answer the specific question about setting values to null. – Shivam Sinha Sep 27 '17 at 16:41

1 Answers1

3

In your example, there is no difference between the two cases. However, consider the following example similar to yours where there is a distinction:

class CacheTest {
  private String test = "testString";
  private String another = "testString";

  public void evictCache(){
    test = null; // this still doesn't remove "testString" from the string pool because there is another strong reference (another) to it.
    System.gc(); //suggestion to JVM to trigger GC 
  }
}

AND

class CacheTest {
  private String test = "testString";
  private WeakReference<String> cache = new WeakReference<String>(test);

  public void evictCache(){
    test = null; // this removes "testString" from the pool because there is no strong reference; there is a weak reference only. 
    System.gc(); //suggestion to JVM to trigger GC 
  }
}
VHS
  • 9,534
  • 3
  • 19
  • 43
  • what if you had `private String test = "testString";` `private String another = "testString";` `private WeakReference cache = new WeakReference(test);` and then tried to do `test = null;`? – AjCodez Dec 02 '18 at 16:52