4

consider this sample code:

1.  public class GC {
2.      private Object o;
3.      private void doSomethingElse(Object obj) { o = obj; }
4.      public void doSomething() {
5.          Object o = new Object();
6.          doSomethingElse(o);
7.          o = new Object();
8.          doSomethingElse(null);
9.          o = null;
10.     }
11. }

When the doSomething method is called, after which line does the Object created in line 5 become available for garbage collection?

A. Line 5

B. Line 6

C. Line 7

D. Line 8

E. Line 9

F. Line 10

Answer: D

why D? it's true that when Line 6 is executed the object created in Line 5 is now referenced by the instance var o and the local var o and when Line 8 is executed the object now is referenced by only the local ref var o, so why the answer is D and what happens after Line 9 is executed?? thanks.

Ramon Dekkers
  • 168
  • 2
  • 9
Eslam Hamdy
  • 7,126
  • 27
  • 105
  • 165
  • A smart JIT could eliminate line 9 entirely. – Louis Wasserman Aug 29 '12 at 16:24
  • It really depends on gc algorithm you are using. I would say the partial gc should clean it up after line 8 method call when it's time for it to make the sweep in eden space. – CoolBeans Aug 29 '12 at 16:27
  • @CoolBeans The algorithm will tell you when it actually gets GC'd; eligibility is determined by the JLS. – corsiKa Aug 29 '12 at 16:28
  • @corsiKa - aah I may have misunderstood the question. It said "when" so I thought that's what the OP is after. – CoolBeans Aug 29 '12 at 16:29
  • It is eligible after it is no longer referenced which would after the re-assignment on line 7 so the correct answer is line 8. Now when it actually gets garbage collected depends on the algoritihm used as CoolBeans pointed out. – Ian Dallas Aug 29 '12 at 16:30
  • It would be after the assignment in line 7, provided the method call above that line didn't cache the pointer somewhere. But of course in this case doSomethingElse DOES cache the pointer, so not until after line 8 has executed (to the point just after the assignment in doSomethingElse) does the object become unreferenced. But the general answer is much simpler: When there are no longer any outstanding references. – Hot Licks Aug 29 '12 at 16:35
  • @LouisWasserman If it were called more than 10,000 times ;) – Peter Lawrey Aug 29 '12 at 16:39
  • Note: Some may claim that the assignment to instance variable `o` makes no difference since there are no other references to that instance variable. But in fact, if an object is considered accessible, all instance variables inside the object are considered accessible, whether or not such access is actually possible. – Hot Licks Aug 29 '12 at 16:46
  • I perceive that some may be confused by the difference between having an object "accessible" and having a reference to it being "anticipated", in a data flow sense. The JVM spec defines GC collectability entirely in terms of accessibility, and whether or not there is some future reference to the object still outstanding in the control flow is immaterial. – Hot Licks Aug 30 '12 at 00:01

2 Answers2

9

The main reason this question is confusing IMO is that there are 2 variables named o. One is the instance variable o and the other is the local variable o inside method doSomething().

Time            instance var o    local var o
Before Line 5:            null               
Line 5:                   null       Object#1
Line 6:               Object#1       Object#1
Line 7:               Object#1       Object#2
Line 8:                   null       Object#2   <- No more references to Object#1

So on (or after executing) line 8, Object#1 is eligible for collection.

Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46
  • the question now is: when Line 7 executes does local Var o no longer refers to Object#1 ? – Eslam Hamdy Aug 29 '12 at 21:56
  • @Eslam -- Local var `o` no longer refers to object 1 after line 7, but instance var `o` does. So the object is not "collectable" until the next line when the call to doSomethingElse nils instance var `o`. – Hot Licks Aug 29 '12 at 22:31
  • But why Local var o no longer refers to object1 after line 7 ?? – Eslam Hamdy Sep 01 '12 at 10:18
  • and how to print this info about ref vars and objects ?? – Eslam Hamdy Sep 02 '12 at 09:11
  • @hot-licks As you said "until the next line when the call to doSomethingElse nils instance var o" don't you think that each object has its own instance variable so the doSomethingElse(null) statement will set instance variable of second object that is created in line 7 – Ajay Sharma Jul 17 '15 at 08:23
  • @AjaySharma - `doSomethingElse`, when not qualified with a reference variable, operates only on the current `this` object. – Hot Licks Jul 17 '15 at 11:45
  • @hot-licks Yes I understood but in line 8 it will operate upon newly created object so it will set instance variable of newly created object to null. – Ajay Sharma Jul 17 '15 at 11:50
  • 1
    @AjaySharma - How does line 8 operate on the "newly created" object?? Where does it get the address from? – Hot Licks Jul 17 '15 at 12:12
  • @hot-licks thanks pal your last comment made it clear to me. – Ajay Sharma Jul 17 '15 at 12:54
1

The question is a bit confusing because it is after line 3 is called the second time that the object is eligible because it clears the second reference o.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130