Java Programming - Garbage Collections - Discussion


void start() {  
    A a = new A(); 
    B b = new B(); 
    b = null; /* Line 5 */
    a = null;  /* Line 6 */
    System.out.println("start completed"); /* Line 7 */
When is the B object, created in line 3, eligible for garbage collection?

[A]. after line 5
[B]. after line 6
[C]. after line 7
[D]. There is no way to be absolutely certain.

Answer: Option D


No answer description available for this question.

Ratish Acharya said: (Mar 2, 2012)  
Answer should be [A].

The truth is that,generally an object becomes eligible for garbage collection in Java on following cases:

1) All references of that object explicitly set to null e.g. object = null

2) Object is created inside a block and reference goes out scope once control exit that block

Ramesh Sukka said: (Mar 14, 2012)  
I think Ans D is right beacuse it depends on definition of method a.s(b); When we pass Object b as parameter it will be call by reference and if b is assigned to some other varilable in method S so b is not eligible for GC after line 5

Yogesh Prajapati said: (Mar 21, 2012)  
Option [A] is correct:

Above question asks for eligibility of object for garbage collection.So as soon as all references of the objects are removed that objects becomes eligible for garbage collection.

But it is totally specific to JVM implementation of garbage collection algorithm when the object will be actually collected by garbage collector.

Raju Rathi said: (May 9, 2012)  
What if A has instant member of type B and in method S, we are storing b into that instant member. In that case, even after line 5 object refered by b will be not eligible for garbage collection.

Shobhit Mishra said: (May 31, 2012)  
@Shobhit Mishra

Option [A] is correct:

An object becomes eligible for garbage collection when it's reference variable is changed(assigned with null in this case)because object is only accessible via reference variable.

Sojin said: (Jul 6, 2012)  
Option D is correct.

We can't be sure until we know the behaviour of s(b) method. What if this reference is being assigned to a static variable of another class ?

Achleshwar said: (Jul 8, 2012)  
Its 'A' because in 3rd line we are declare the function and in 5th line we assign the value of the function.

Vishal Wakchaure said: (Aug 1, 2012)  
Its 'D'. Because JVM garbage collector uses Timer Interrupt for doing garbage collection. That time is specific to that JVM. Although at line 5, it sees as there is no reference to object B, but we can not actually tell, when that object will be collected.

Ranjani said: (Aug 23, 2012)  
It's A. Java is never pass by reference..that's a C language concept..

Mahi said: (Aug 29, 2012)  
Can anyone tell(THOSE WHO SAY D I CORRECT) through which reference variable we can access object of B.. coz after 5th line b is pointed to null.. this implies there is no further reference to object.. it may b possible that it is being used in the method s.. but still that method gets executed before the 5th line... and 5th line gets executed only after we are out of the method "s".. so THERE IS NO POSSIBLE REFERENCE TO THE OBJECT b...


@Ranjani: u r wrong.. whenever an object is passed it is done via call by reference.. yeah its true that in general we don't pass arguments via call by ref. in java but.. object is done via call by ref. only...

Ravi Kapoor said: (Sep 11, 2012)  

I'm pretty much with you on that. The method taking reference variable 'b' must have been executed before referencing it to null. So b has to be eligible for garbage collection only after line 5. I too want answers those who say D.

Ashish Mishra said: (Dec 23, 2012)  
option A

Just bcz of when object reference bcome null then it will eligible to call runtime.gc();

Chandan Kumar said: (Jan 13, 2013)  
The garbage collector does not indicate when it is going to be called. Hence you can never be certain when GC will be called.

Oliver Meyer said: (Jan 26, 2013)  
Make class A have a static member and make A.s(B) assign its parameter to that member. Then, after the end of method start you can access the object created in line 3 as A.storedB Thus, D is correct.

public class A {
public static B storedB;
public void s(B b) {
storedB = b;

BTW: The question is not "When will the object be garbage collected?" The Question is: "When is it eligible for garbage collection?

Ormek said: (Jan 29, 2013)  
The Question is about being *eligible for garbage collection*. Thus it does not matter, when the object actually gets collected. And yes, there is no guarantee that the garbage collector will ever collect a certain object.

Ankur said: (Feb 14, 2013)  
That is totally depend on the jvm implementation and algorithm that provide how and which time to rin gc() method we don't need to worry about it.

Prabavathi.V said: (Mar 15, 2013)  
Actually, garbage collection object values are always unused and it will never be absolutely certain.

Shankar Shingare said: (Jul 10, 2013)  
Option A is correct.

1. It is ask for garbage collection, so when no possible reference to object, it is eligible for garbage collection i.e. after line 5.

2. It may b possible that it is being used in the method s. But still that method gets executed before the 5th line. And 5th line gets executed only after we are out of the method "s". So there is no possible reference to object b.

Bineeth said: (Jul 29, 2013)  
Actually, Garbage Collection in Java is Unpredictable. So we can say both options A and D are correct.

Rajesh Kumar Nayak said: (Aug 19, 2013)  
I think option D is correct because the definition of s(b) method is not given here. It might be the case that A has a class level variable and in s(b) the class A might be assigning the value to the class level variable.

So the reference of b would still exist even after we set a=null;

Raman said: (Sep 11, 2013)  
Ans: D is correct.


Object A,B;
A=new Object();

In the above scenario only one Object is created that is pointed by two references as A, B .

If A=null it is not eligible to GC because B is pointing Object.

Vikesh said: (Sep 17, 2013)  
Consider the following:

class A {
public void s(final B b) {
new Thread() {
public void run() {
//This is a long running thread. doing something on b

class B {
public void doSomething() {

class MyAwesomeClass{
void start() {
A a = new A();
B b = new B();
b = null; /* Line 5 */
a = null; /* Line 6 */
System.out.println("start completed"); /* Line 7 */

Now, I think things should get pretty clear. There is obviously no way it can be told. Even after line 7 has executed, B (created at line 3) might not be eligible for garbage collection. More so, it is possible that object isn't eligible even after start() has finished. We are talking about object not the reference to object. All that is happening above is reference to object is made null - the real object may still be there somewhere, which we don't know.

Aaron said: (Sep 25, 2013)  
The answer is D because the question is asking when the OBJECT created for B is eligible for garbage collection. When a.s(b) is invoked, a copy of the reference 'b' is made and passed into a.s(...) (all Java is pass by value!).

This means that there is now a reference to the original OBJECT floating about somewhere, and that object could now be referenced in a list or map (or something) somewhere well after the method that constructed it is complete.

Amit said: (Oct 11, 2013)  
It will throw an error while creating the reference of the object(a.s(b)).

Sang Dang said: (Oct 11, 2013)  
void start() {
A a = new A();
B b = new B();
b = null; /* Line 5 */
a = null; /* Line 6 */

I also think that the answer should be D.

1. If the function a.s(b), keep the pointer to b object so then the b should eligible for GC after line 6.

2. If the function do nothing, so b should eligible for GC after line 5.

So my answer is D.

KotteMani said: (Nov 30, 2013)  
Here b object is created and then called the parameter after that they assigned to null so there is a chance of garbage collection but the answer is D because already b object is created.

My answer is: D.

Winston said: (Jan 24, 2014)  
It's definitely D. I even profiled the example and saw that object B still stays in memory ONLY IF a.s(b); method stores b into a static variable.

Fayyam said: (Jul 11, 2014)  
Can anybody (specially those who are saying that option D is correct ans) tell me after line 5th can we call any instance member of class B by using reference b.

Mangesh Ashok Shingate said: (Jul 30, 2014)  
We do not know about either object which assigned to object reference 'b' is assigned to another object reference of the class or not, if object is assigned to some other reference then answer is D, if object is not assigned to any other object reference then answer is A.

So until understanding execution of method s (i.e: a.s(b)), There is no way to be absolutely certain to say when object assigned to b is eligible for garbage collection.

So answer D is correct.

Arnold Villasanta said: (Aug 10, 2014)  
Yup, D is correct.

No one knows how instance 'a' used 'b' in its method.

Ramaswamy said: (Aug 31, 2014)  
I think D is correct.

1. In Java you always pass by value, which in this case means a copy of the reference to object 'b' is made and passed to the function s.

2. As many have pointed out above, in function 's', many things could be happening. You may be setting a static variable in another class (or even the enclosing class of the present start() function) to be equal to this reference. You could also be starting a new thread in function 's', which may be using the reference. That thread could continue to execute even after the program has left the current start() function.

Therefore, with the information given to us, you cannot say when the object pointed to by 'b' will be eligible for garbage collection.

Gayachand Sahoo said: (Sep 16, 2014)  
Answer : A.

Because All method call will stored in the stack area. And object created in the heap area. So when method S of A class executed at.

That moment b object lost for that method only but b object will also.

Be alive in the Heap area.

Object b will be garbage collected when we explicitly mention that ref as null.

Sushank Dahiwadkar said: (Oct 4, 2014)  
According to me option D is correct because there is no definition for function s(). and in function s() another variable may be getting allocated to b's reference.

So D is correct.

Dong said: (Oct 8, 2014)  
D is correct, it depends on the implementation of A.s(). e.g. in the following case, b is not eligible for gc after line #5:

class A {
B bInA;
public void s(B b) {
bInA = b;

In this case, b is only eligible for gc after line #6, when A is also eligible for gc.

Fayyam said: (Jan 7, 2015)  
Normal method call hold the control till then its execution not completed(unlike run() method in multithreading programming ) so what ever the implementation of a.

s() once that line will be encounter by JVM control will go to the class A and start the execution of s() method and after completing its execution it will again come back to line number 5 where object b assign with null. If you try to use object b after line no 5 you will get NullPointerException.

So Answer A is correct.

Konapuram Malli said: (May 19, 2015)  
Option A is correct because of that object reference is null.

Mit said: (Jul 11, 2015)  
Option A is correct Even if we assign that object reference to some other B = null removes from B. Therefore b will be eligible after line 5.

Anuja said: (Nov 18, 2015)  
Please explain about option D.

Anchal Panda said: (Jan 17, 2016)  
Option A is correct.

As soon as reference b is set to null, b object becomes eligible for garbage collection. The link is removed as soon as it points to null.

Erik said: (Jan 27, 2016)  
Option D is correct.

Assume a.s (b) adds a reference to the object that b references to a collection of some sort, then that object does not become eligible after Line 5.

Mostapha said: (Mar 11, 2016)  
Option D.

It's depends of what a.s (b) will do if it exists.

Pankaj said: (Mar 27, 2016)  
Option D is Correct.

Because Object A hold the Object B, and when B is null still Object A has a reference.

So there is no chance that Object B garbage Collected at any point.

But after execution of line, no 6 both Object is eligible for GarbageCollection.

Xinu said: (May 3, 2016)  
If the 'b' is assigned, by some different reference, in side the method s(b), then, 'b' will not come across garbage collection.

Anish Kumar said: (Aug 25, 2016)  
I think no any option is a right answer. Because when we are calling s method then there is a possibility of reassignment of the object in method but it is local, it will be executed by execution engine of java after completion of (s) method all object will be eligible for GC. So just before line number 5, all b object will be eligible for GC. Please, anyone explain to me if I am wrong.

Nikhil Nambiar said: (Aug 30, 2016)  
Option D is right because there is no assurance of garbage collector activity. And we cannot even predict the implicit call to finalize method.

Mukesh said: (Oct 18, 2016)  
Option D is correct Because JVM runs garbage collection without knowing of the user. So, even if we run the finally we just remove some resources, not the complete object is removed.

Yuvaraju said: (Dec 7, 2016)  
We can assign a null to the object, so option D is correct.

Mayank said: (Jan 5, 2017)  
We are passing value null for reference of Object not to object, so Object will be available for garbage after line 5, after line 5 no one access Object of B class OR we can say we will not be able to access instance member of B class we can only access Reference of B Class.

So, my answer is A.

TheExodu5 said: (Mar 20, 2017)  

The reference was passed to A.s. That reference may still exist, and the original object can still be accessed.

The answer is D.

Shailesh Suryawanshi said: (Aug 4, 2017)  
Here the object is created at line 3 by the new method. And this object created is referenced by b.

This b is then used by the method of a.

The object is eligible for garbage collection if and only if all the references to that object is set to null. That is De-referencing the object.

But since the object is passed in method and it may have its valid use that the object in the method is referenced by some other object reference say BRefernece=b;.

Since the object is on Hip memory and it is referenced by BReference. But though the other reference b is set to null the object remains on heap with a reference b.

The Analogy would be you have a remote control for tv and that tv you have given to your friend. Your tv had 'b' remote control at your home. Let's say you forgot to give remote to your friend. Mean while your friend bought new remote 'B_Reference'. Since your remote control can't operate the tv that means it is set to null. And the tv has got new remote control 'B_reference'. That means your tv is accessible. Implies that you will not give the tv to bhangarwala. That is garbage collection.

If the tv did not had remote control it was eligible for garbage collection, but you have the remote control that's why it is not eligible for garbage collection.

Anomi said: (Sep 7, 2017)  
Option [B] is most accurate.

We do not know what exactly method s does but let's suppose that it creates reference to b in a. Then gc cannot destroy b cause we still have object a that has reference to b. Therefore we need to get rid of object a, and then after line 6 we can be sure that object b is eligible for gc.

Sasikanta said: (Oct 26, 2017)  
(A) is correct arguably. There is no reference running after this line.

Here the question is "when object b is eligible for garbage collection" not system.gc() will collect it or about its timing. So if the question is about eligible then no doubt it is after line five, which is a basic rule (by Effective Java [Joshua Bloch] ).

Turgay Ekici said: (Nov 6, 2017)  
(A) Correct,

If the question was in which point that JVM will collect, the answer would be (D).

But the question is "when the object is eligible to be collected".

Zafar said: (May 19, 2019)  
A is correct , because while passings(b) we just pass the value of whatever in b which will further get collected in method S(B newXyz) so there is no b at all, after line 5, b is referred to null and will be GC'd.

Angelus said: (Jul 18, 2019)  
An object becomes eligible for garbage collection if there is no reference to it or if it has been assigned to null.

So, line 5, assign obj B to null, so I think A is the answer.

Vikash Kumar said: (Dec 30, 2019)  
Yes D should be the best answer. Because the we need to understand b is not an object but a reference variable which holds the reference of the an object of type. Since it assigned to null in line no 5 it doesn't mean that it couldn't be assigned to other reference variable. It totally depends on the definition of the method s(). That is why in my view D is the best answer.

Utkarsh said: (Jun 5, 2020)  
I believe answer should be B.

Clearly we still have a.s referencing B object (new B()) and only after a is set to null the whole chain would break.

Chandra Mohan Yadav said: (Jun 21, 2022)  
public class Loan
public static void main(String[] args)
Loan loan1 = new Loan();
Loan loan2= new Loan();
Loan loan3 = new Loan();
Loan loan4 = new Loan();


loan3 = null

How many objects are eligible for garbage collection?

Can anyone explain this?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.