1

I'm doing some exercices from a java certification book. In a question about the Garbage collection, they present the following code:

class Test {
   private Demo d;
   void start() {
      d = new Demo();
      this.takeDemo(d);
   }
   void takeDemo(Demo demo) {
      demo = null;
      demo = new Demo();
   }
}

And the question is

When is the Demo object, created on line 4, eligible for garbage collection?

I'd say it can be collected after the instruction demo=null; because there are no longer references to it, but in the answer they give is:

When the instance running this code is made eligible for garbage collection

What am I missing?

Luis Sep
  • 2,384
  • 5
  • 27
  • 33

6 Answers6

5

Java is "pass-by-value", so this statement demo = null; only affects the local copy of demo in takeDemo. It does not set d to null, which is therefore reachable as long as the enclosing instance is reachable.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 4
    @Dukeling It is not the case. – assylias Feb 08 '13 at 12:27
  • If you compare Java and C++, Java fits better into pass-by-pointer (basically exactly). You could say it's pass-by-value of a pointer, but then you're just complicating things more than necessary (and then you could say the same in C++). Note I'm not arguing with the rest of your answer. – Bernhard Barker Feb 08 '13 at 12:31
  • 2
    For me, in terms of with C/C++, Java implements pure pass by value. – Mikhail Vladimirov Feb 08 '13 at 12:32
  • @Dukeling A reference is very similar to a pointer, but when you pass a reference to a method, it is the value of the pointer, not the pointer itself, which is passed. [This tutorial](http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) seems to disagree with your claims... – assylias Feb 08 '13 at 12:34
  • Agree to disagree then (apparently with everyone). I know Java well, I know C++ well and I see no difference between pass-by-pointer in C++ and how Java objects are passed. – Bernhard Barker Feb 08 '13 at 12:40
  • Alas! I also thought Java passed Objects by reference. – Luis Sep Feb 08 '13 at 12:48
  • @Perception Unless you understand it as that all Java objects are actually pointers. – Bernhard Barker Feb 08 '13 at 12:52
1

You correctly noted that demo is set to null. But d isn't. It still holds a reference to the same object. Hence the answer.

mikołak
  • 9,605
  • 1
  • 48
  • 70
1

Because demo = null just changes value of method parameter demo, which is local to the method, and does not affect value of field d. Remember that in Java all method parameters are passed by value.

Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
0

The reference to local variable d is 'active' until the method start() has finished. Before that, the garbage collection should not free it.

akaIDIOT
  • 9,171
  • 3
  • 27
  • 30
0

Demo from 4th line can be collected only with Test instance together. Or if you call start method again (old Demo will be collected and new created)

takeDemo method has no effect on first Demo. But it creates second Demo. Second Demo can be collected immediately after takeDemo method is finished.

Leonidos
  • 10,482
  • 2
  • 28
  • 37
0

when you call this.takeDemo(d); the value of reference d is copied to parameter demo so d and demo both reference to the object created in line 4. Now inside the method when you are setting the variable demo to point to null d still holds a reference and hence the object will be eligible for garbage collection once the Instance of Test class is garbage collected.