4

In a Java setter method that assigns a new Object to a field such as:

public class MyClass
{
     private String s;
     public void mySetter(String newS) {
          s = newS;
     }
}

Does the previous String s get garbage collected after mySetter is called or should I null out s before I assign it to the new value?

public class MyClass
{
     private String s;
     public void mySetter(String newS) {
          s = null;
          s = newS;
     }
}
Steven Roberts
  • 300
  • 1
  • 3
  • 14
  • It appears that I do not need to null it out, but I would greatly appreciate references, like the JLS, to support these claims. – Steven Roberts Dec 11 '13 at 17:51
  • Setting a variable to something and immediately setting it to something else is useless in most (all?) cases. Oh, now I see your edit. It's needed there because the position element[--size] is going to sit there unused for some time until someone inserts another object in the ArrayList. – marcus Dec 11 '13 at 17:57
  • Well, it is not necessarily going to be called immediately. For example, if a user presses a button in an application it could set a new String s for MyClass. – Steven Roberts Dec 11 '13 at 18:01
  • @marcus I took that edit back out because I realized the difference in my code and the ArrayList's code – Steven Roberts Dec 11 '13 at 18:04

4 Answers4

3

Garbage collection will only happen when memory space is required by JVM

so in your case it will not be garbage collected as soon as your method gets called.

for more already an answer here

Also while assigning string to another value, you need not to set it to null first, as when you assign a new value to string means that now your reference variable is not pointing to previous value of the String object but to the new one and Java provides you the flexibility of not worrying about GC like other programming language. So don't try doing GC, Java can take care of it for you

Community
  • 1
  • 1
dev2d
  • 4,245
  • 3
  • 31
  • 54
3

no need to null out, the garbage collector will find the string if no one else references it.

aepurniet
  • 1,719
  • 16
  • 24
  • Could you provide a source to back up your statement? Thanks – Steven Roberts Dec 11 '13 at 17:45
  • its the nature of garbage collection. you can test this by overriding the `finalize()` method of an object to log something. this method is called right before the object is garbage collected. – aepurniet Dec 11 '13 at 17:59
3

Does the previous String s get garbage collected after mySetter is called or should I null out s before I assign it to the new value?

If your previous String s is not referenced anywhere then it will be. But it won't happen immmedialtely after mySetter is called. No need to set it to null.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186
3

You don't have to do s = null; part. In Java a variable is in fact a reference to physical object in RAM memory. So when you do s = newS; you make a variable s point to a new object in RAM and the old object is no longer referenced by any of your variables and will be garbage collected.

Flying Dumpling
  • 1,294
  • 1
  • 11
  • 13