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.
Dong said:
1 decade ago
D is correct, it depends on the implementation of A.s(). e.g. in the following case, b is not eligible for gc after line #5:
--------
class A {
B bInA;
public void s(B b) {
bInA = b;
}
}
--------
In this case, b is only eligible for gc after line #6, when A is also eligible for gc.
--------
class A {
B bInA;
public void s(B b) {
bInA = b;
}
}
--------
In this case, b is only eligible for gc after line #6, when A is also eligible for gc.
Chandra Mohan Yadav said:
3 years ago
public class Loan
{
public static void main(String[] args)
{
Loan loan1 = new Loan();
Loan loan2= new Loan();
Loan loan3 = new Loan();
Loan loan4 = new Loan();
loan1=loan3;
loan2=loan4;
loan3 = null
}
}
How many objects are eligible for garbage collection?
Can anyone explain this?
{
public static void main(String[] args)
{
Loan loan1 = new Loan();
Loan loan2= new Loan();
Loan loan3 = new Loan();
Loan loan4 = new Loan();
loan1=loan3;
loan2=loan4;
loan3 = null
}
}
How many objects are eligible for garbage collection?
Can anyone explain this?
(3)
Pankaj said:
9 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.
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.
Ravi Kapoor said:
1 decade ago
@Mahi.
I'm pretty much with you on that. The method taking reference variable 'b' must have been executed before referencing it to null. So b has to be eligible for garbage collection only after line 5. I too want answers those who say D.
I'm pretty much with you on that. The method taking reference variable 'b' must have been executed before referencing it to null. So b has to be eligible for garbage collection only after line 5. I too want answers those who say D.
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
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.
Ormek said:
1 decade ago
The Question is about being *eligible for garbage collection*. Thus it does not matter, when the object actually gets collected. And yes, there is no guarantee that the garbage collector will ever collect a certain object.
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.
Zafar said:
6 years ago
A is correct , because while passings(b) we just pass the value of whatever in b which will further get collected in method S(B newXyz) so there is no b at all, after line 5, b is referred to null and will be GC'd.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers