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 2 of 6.
Ramaswamy said:
1 decade ago
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.
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.
Fayyam said:
1 decade ago
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.
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:
1 decade ago
Option A is correct because of that object reference is null.
Mit said:
1 decade ago
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:
10 years ago
Please explain about option D.
Anchal Panda said:
10 years ago
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.
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:
10 years ago
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.
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:
10 years ago
Option D.
It's depends of what a.s (b) will do if it exists.
It's depends of what a.s (b) will do if it exists.
Pankaj said:
10 years ago
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.
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:
9 years ago
If the 'b' is assigned, by some different reference, in side the method s(b), then, 'b' will not come across garbage collection.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers