Java Programming - Garbage Collections - Discussion
Discussion Forum : Garbage Collections - General Questions (Q.No. 1)
1.
void start() {
A a = new A();
B b = new B();
a.s(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?Discussion:
59 comments Page 4 of 6.
Mangesh Ashok Shingate said:
1 decade ago
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.
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.
Fayyam said:
1 decade ago
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.
Winston said:
1 decade ago
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.
KotteMani said:
1 decade ago
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.
My answer is: D.
Sang Dang said:
1 decade ago
void start() {
A a = new A();
B b = new B();
a.s(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.
A a = new A();
B b = new B();
a.s(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.
Amit said:
1 decade ago
It will throw an error while creating the reference of the object(a.s(b)).
Aaron said:
1 decade ago
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.
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.
Vikesh said:
1 decade ago
Consider the following:
class A {
public void s(final B b) {
new Thread() {
public void run() {
b.doSomething();
//This is a long running thread. doing something on b
}
}.start();
}
}
class B {
public void doSomething() {
}
}
class MyAwesomeClass{
void start() {
A a = new A();
B b = new B();
a.s(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.
class A {
public void s(final B b) {
new Thread() {
public void run() {
b.doSomething();
//This is a long running thread. doing something on b
}
}.start();
}
}
class B {
public void doSomething() {
}
}
class MyAwesomeClass{
void start() {
A a = new A();
B b = new B();
a.s(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.
Raman said:
1 decade ago
Ans: D is correct.
ex:
Object A,B;
A=new Object();
B=A;
A=null;
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.
ex:
Object A,B;
A=new Object();
B=A;
A=null;
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.
Rajesh Kumar Nayak said:
1 decade ago
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;
So the reference of b would still exist even after we set a=null;
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers