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 1 of 6.
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
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
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.
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.
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.
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 ?
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.
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.
Ranjani said:
1 decade ago
It's A. Java is never pass by reference..that's a C language concept..
Mahi said:
1 decade ago
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...
AND
@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...
AND
@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...
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers