-4
private Student student = new Student();

public Student getStudent(){
    return student;
}


public void function(){
   getStudent().setName("john");
}

public void function(){
   Student student_local = getStudent();
   student_local.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?

I simple word GC will be called for CASE-1 or not?

Amit Yadav
  • 32,664
  • 6
  • 42
  • 57

2 Answers2

1

Amit i've seen your other question, what matters is always only reachability, it doesn't matter how you use student, it will always be reachable until the class that contains it is reachable.

Update, regarding the question you added:

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

Neither because you do the same thing in both functions and remember that you can't really know when your objects will be collected, and it doesn't even matter for this kind of examples.

Read this description of how GC works: http://www.cubrid.org/blog/dev-platform/understanding-java-garbage-collection/

Edited: Should learn to read the questions, read this revision of my answer, sorry Amit.

uraimo
  • 19,081
  • 8
  • 48
  • 55
  • .@uraimo thanks for this wonderful link and about question that I would say, please consider this method we are calling in a loop. I am a Android developer where each application is assign some limited memory and when app grows up near to threshold GC must be called – Amit Yadav Mar 16 '15 at 16:40
  • For the second function it doesn't matter if it's in a loop, as soon as you are outside of that function the reference will be eligible for garbage collection. In the first case, well, the object will be around until someone point to the class containing that private Student, if you put that the variable referring to that to null or make it point to another StudentOuterClass than the private Student will be eligible for GC. – uraimo Mar 16 '15 at 16:47
  • 1
    sorry but in the second case no new object is created, the method operates on an instance variable, thus GC will not collect the object after method returns – akhilless Mar 16 '15 at 16:47
1

In all the three cases method function() operates on an instance variable that was instantiated at the time of class initialization. That means that instance variable student will continue to hold a reference to the object Student after the method function completes in all cases and thus the object will not become a candidate for garbage collection. That also means that the efficiency of those code snippets in terms of garbage collection will be identical (since the GC will not collect the student object after the methods complete in all cases)

akhilless
  • 3,169
  • 19
  • 24
  • .@akhilless you mean to say that here GC will not call for either of the case? – Amit Yadav Mar 16 '15 at 16:48
  • Student will be collected when the class that contains that private instance will be collected. – uraimo Mar 16 '15 at 16:53
  • 1
    @AmitYadav not exactly. I mean after the method `function()` finishes in all the cases the object will still be reachable (instance variable will continue to hold a reference on it) and hence not be garbage collected. The GC may occur but it will not collect the student object. The timing of garbage collection depends on the particular GC used. – akhilless Mar 16 '15 at 16:56