0
public void function(){
   new Student().setName("john");
}

public void function(){
   Student student = new Student();
   student.setName("john");
}

Does GC behave differently for both of the snip?

I mean which case (CASE-1/CASE-2) is more GC efficient in terms of Time?

Amit Yadav
  • 32,664
  • 6
  • 42
  • 57
  • what makes you think they are different? – Jason Hu Mar 16 '15 at 16:01
  • Please stop constantly changing your question. You may invalidate all the answers. If you have follow ups, edit your question and **add** them. – Sotirios Delimanolis Mar 16 '15 at 16:12
  • @AmitYadav You can't change the definition and wording of your question radically. Make a new question. People may not always check to see if the question has changed completely, which may also end up making their answer irrelevant. It just amounts to being a good citizen on SO. – Vivin Paliath Mar 16 '15 at 16:16
  • @VivinPaliath I created a new question in same context what I looking for http://stackoverflow.com/questions/29081859/java-anonymous-object-and-garbage-collection-part-2 – Amit Yadav Mar 16 '15 at 16:35

3 Answers3

4

Does GC behave differently for both of the snip?

No. Once the setName method has been invoked, the Student object created with new Student is no longer reachable and can be garbage collected.

I mean which case (CASE-1/CASE-2) is more GC efficient in terms of Time?

Neither is more efficient. The first case will have one less assignment in the bytecode. This does not affect GC whatsoever.

From the JLS

A reachable object is any object that can be accessed in any potential continuing computation from any live thread.

In both snippets, after the invocation of setName, the Student object is no longer reachable (assuming the constructor and the setName method don't leak references to the object - but even in that case, the behavior of both snippets would be the same).

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • What happens if the Student object runs code inside of it, in a new thread? – Miguel Jiménez Mar 16 '15 at 16:04
  • @jachinte Both snippets would still be equivalent because both execute the same methods. – Sotirios Delimanolis Mar 16 '15 at 16:05
  • Thanks for the JLS reference. I was also looking for that. +1 – Vivin Paliath Mar 16 '15 at 16:08
  • @SotiriosDelimanolis In the `new Student()` case where there is no assignment, how can that object be further accessed in the method? I was trying to find something that talks about that. If you don't have a variable that holds a reference to that instance, how would you work with/access that instance again? – Vivin Paliath Mar 16 '15 at 16:10
  • @VivinPaliath You wouldn't. You can only use the reference on the stack (from the `new` expression) for that one method invocation. (Is that what you're asking?) – Sotirios Delimanolis Mar 16 '15 at 16:12
  • @SotiriosDelimanolis Yeah, that was basically what I was asking, as a clarification to what the JLS says. In this case, does using that reference one time (i.e., without assignment) count as "access" for the life-time of that method? I was trying to tease out any nuance between the two cases with respect to what the JLS says. Technically we don't have any references to `Student` in the first case that we can use for further access, but I'm assuming that doesn't affect the GC until control exits the method, which is why the GC behaves the same in both cases? – Vivin Paliath Mar 16 '15 at 16:14
  • @VivinPaliath GC can technically claim the `Student` object before execution leaves the method. It can technically claim it right after the `setName` method is invoked and executions returns. – Sotirios Delimanolis Mar 16 '15 at 16:21
  • @SotiriosDelimanolis Ok, that makes sense. That's what I was looking for! The difference between the two is that in the first case, `Student` can potentially be GC'ed right after that one statement, whereas in the second case it would be after the `setName`. I guess another way to frame my question would have been "is an object that is constructed without assignment to a variable, considered to be unreachable after it has been initialized". – Vivin Paliath Mar 16 '15 at 16:26
  • in the second case the object is still reachable by the current thread after `setName` returns because it is still on the stack and remains their until the method `function` returns. – akhilless Mar 16 '15 at 17:07
2

In the first case you don't assign the newly created object to a variable, hence it becomes unreachable for the code (and thus becomes a candidate for garbage collection) as soon as setName (String name) method returns.

In the second case local variable student will prevent the student object from being garbage collected until it goes out of scope. In other words, in the second snippet the student object will continue to be a live object after setName(String name) returns and will become a candidate for garbage collection only after the method function() returns.

UPDATE:

In terms of the time required for garbage collection both cases are equal since in all of them you end up having one garbage object.

akhilless
  • 3,169
  • 19
  • 24
  • Scope and GC are unrelated concepts. – Sotirios Delimanolis Mar 16 '15 at 16:10
  • sorry i am not agree. In this particular 2nd case the student object becomes unreachable after the student variable goes out of scope and hence no live reference to the object remains. Please counter my logic or I will consider your downvote pointless – akhilless Mar 16 '15 at 16:13
  • 2
    GC is driven by runtime reachability. [Scope](http://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.3) is a compile time concept that drives what names you can use in your source code. – Sotirios Delimanolis Mar 16 '15 at 16:19
  • @akhilless can you please answer to this question too http://stackoverflow.com/questions/29081859/java-anonymous-object-and-garbage-collection-part-2 – Amit Yadav Mar 16 '15 at 16:34
  • 1
    This answer is wrong. The object becomes a candidate for GC before the `function` method returns. – Sotirios Delimanolis Mar 16 '15 at 16:47
  • @SotiriosDelimanolis why it becomes a candidate before method returns? the local variable will remain on the stack untill the method finishes its execution and hence the object will still be reachable until the method finishes its execution which destroys its stack with all of its local variables. Once it happens student object becomes unreachable and hence becomes a candidate for garbage collection - after the method returns. – akhilless Mar 16 '15 at 16:51
  • The JVM is smarter than that. It knows that after `setName` is invoked, there is no _potential continuing computation_ that can access the object referenced by `student`. It therefore becomes a candidate right away, before `function` returns. – Sotirios Delimanolis Mar 16 '15 at 16:57
  • @SotiriosDelimanolis I got your point. Is there any documentation you could link that backs up that claim explicitly? I mean I did not encounter this statement in the Memory Management White Paper. Moreover, all other resources I have seen claim that local variable make the object remain to be reachable until they are destroyed (hence until the method returns). So it might be true for some implementations of the GC but not true for all others. – akhilless Mar 16 '15 at 17:04
  • 1
    [This](http://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.6.1) JLS chapter explains reachability of an object. A JVM implementation can chose (maybe because it's easier) not to look forward and simply use (in this example) the local variable table as a indicator of reachability. However, from a language point of view, the object is unreachable and therefore a candidate for GC. – Sotirios Delimanolis Mar 16 '15 at 17:08
0

Answer for question is no.

Gc behaves nearly same for both cases.

Garbage Collector has a unpredictable behavior. But Any Object which is no longer referred or is no longer in use is eligible for garbage collection.

Case 1 : Main objective of anonymous object is for instant use (one time use). So after line "new Student().setName("john");" , your anonymous object is not in use so it will be GC.

case 2 : Student student = new Student();

student.setName("john");

After this line student reference is no longer referred so it will be GC.

There are few chances that in case 2 student reference may be leaked but GC is smart enough to handle this.

Now in case 1 if you want object for one time use then go for anonymous object as Objects are created in heap memory and GC sweep heap memory. Stack memory are managed in such way that memory used by stack is reclaimed automatically.

You can referred this link for more.

Community
  • 1
  • 1
Harshil
  • 69
  • 1
  • 2