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 6 of 6.
Ranjani said:
1 decade ago
It's A. Java is never pass by reference..that's a C language concept..
Vishal Wakchaure said:
1 decade ago
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.
Achleshwar said:
1 decade ago
Its 'A' because in 3rd line we are declare the function and in 5th line we assign the value of the function.
Sojin said:
1 decade ago
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 ?
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 ?
Shobhit Mishra said:
1 decade ago
@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.
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.
Raju Rathi said:
1 decade ago
@Yogesh,
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.
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.
Yogesh Prajapati said:
1 decade ago
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.
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.
Ramesh Sukka said:
1 decade ago
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
Ratish Acharya said:
1 decade ago
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
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers