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 3 of 6.
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.
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.
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.
Amit said:
1 decade ago
It will throw an error while creating the reference of the object(a.s(b)).
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.
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.
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.
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.
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.
Arnold villasanta said:
1 decade ago
Yup, D is correct.
No one knows how instance 'a' used 'b' in its method.
No one knows how instance 'a' used 'b' in its method.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers