4

I am working on the OCA Java certification and unsure of how to understand the answer to one question.

public class Test {

public static void main(String[] args){
    Student s1 = new Student("o1"); 
    Student s2 = new Student("o2");
    Student s3 = new Student("o3");
    s1=s3; 
    s3=s2;
    s2=null;
    //HERE
    }
}

The question is which object will be available for garbage collection after the //HERE point.

The answer provided by the online test is : One object (o1).

Can someone explain me why?

a1a1a1a1a1
  • 151
  • 12
  • You should read the book ```OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808``` by Jeanne Boyarsky and Scott Selikoff – Ashish Mishra Jul 09 '21 at 10:57

4 Answers4

7

Consider a simple class Student.

Step 1 :

Student s1 = new Student("s1"); 
Student s2 = new Student("s2");
Student s3 = new Student("s3");

s1, s2, s3 are referencing 3 object in the heap space

enter image description here

Step 2 :

Student s1 = s3; 

s1 is now referencing the s3 object in the heap space

the object s1 in the heap space has lost his reference

enter image description here

Step 3 :

Student s1 = s3;
Student s3 = s2;

Variable s1 reference s3 heap space

Variable s3 reference s2 heap space

enter image description here

Step 4 :

Student s1 = s3;
Student s3 = s2;
Student s2 = null;

Variable s1 reference s3 heap space

Variable s3 reference s2 heap space

Variable s2 lost his reference (null pointer)

enter image description here

Conclusion :

After line 11, one object is eligible for garbage collection

Boris S.
  • 1,068
  • 1
  • 8
  • 18
  • I just wanted to know what will happen if there's a string let say String interest = "Q&A on Stackoverflow" and later on I do interest = null so will it garbage collected as we know that String is a immutable class in java and string's are stored on String Pool on Heap what are preserved even after the program execution that's why we avoid storing password's in String non primitives as they can be in memory and later on can be hacked using the memory dump, please enlighten me! – Ashish Mishra Jul 09 '21 at 11:02
  • you are quoting "line 11" and _exactly_ where is that in the question or your answer? IF you mean after the comment `HERE`, then you answer is highly incorrect. _everything_ after that line is eligible for GC. – Eugene Jul 09 '21 at 14:24
  • @a1a1a1a1a1 [read this comments under the question](https://stackoverflow.com/questions/68213545/how-many-objects-are-created-and-how-many-eligible-for-garbage-collection) and understand that there is a difference between answering that exam question and reality. Those GC exam question are inaccurate, at best. – Eugene Jul 09 '21 at 14:27
  • 2
    Besides the confusing reference to “line 11”, using two-headed arrows is wrong. Objects don’t have back-references to the local variables. – Holger Jul 27 '21 at 10:55
4

Every time these kind of questions pop-up, the answer is still the same: after the comment HERE, every single object is eligible for garbage collection. Nothing is used after that line, thus no strong reference exist to any object, thus everything is eligible for GC. Unfortunately, these kind of questions make sense only in the context of getting the correct "points" for the exam, thus people learn them the way they are. The reality is that without a broader context of reachability, they only confuse users, imo.

Think about - are there are any live references to any of your objects after that comment? No. As such, is every single instance eligible for GC? yes. And note that they are eligible after that comment, not after the methods ends. Do not mash scope an reachability together.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • Thanks for your answer. I understand your point. However in the context of the exam I think the answer Boris provided fits more what is expected as an answer. And I agree what a weird question, I wasnt expecting this exam to be that much decieving in its wording. – a1a1a1a1a1 Jul 09 '21 at 21:43
  • 2
    The fact that the exam’s expected answer is wrong highlights the even bigger logic error of such questions: “Java (and other modern programming languages) implement garbage collection, so the developer doesn’t need to deal with it, as even experts sometimes get it wrong—so let’s force beginners to play garbage collector and deal with memory management in a way they’ll never have to do again in their life”. – Holger Jul 27 '21 at 10:52
1
Student s1 = new Student("o1"); 
Student s2 = new Student("o2");
Student s3 = new Student("o3");
s1=s3; // The "s1" variable now points to the object referenced to by s3, ie "o3"
s3=s2; // The "s3" variable now points to the object referenced to by s2, ie "o2"
s2=null; // The "s2" variable points to nothing

At the end of the execution, objects "o3" and "o2" are referenced by variables ("s1" and "s3" respectively). As such, the object "o1" is not pointed to by any variable, and is available for destruction by the garbage collector.

Chris Neve
  • 2,164
  • 2
  • 20
  • 33
1

After exiting the method all object will be eligible for garbage collection, since s1,s2,s3 are local variables and references are not passed outside.

However on the last line of the method, s1 holds reference to o3, s2 points to nowhere, s3 points to o2. Only o1 has no reference pointing on him, therefore it is eligible for garbage collection.

Saginatio
  • 99
  • 4