This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
this question is from examguide.com class C { public static void main(String a[]) { C c1=new C(); C c2=m1(c1); C c3=new C(); c2=c3; //6 anothermethod(); } static C m1(C ob1){ ob1 =new C(); return ob1; } } After line 6, how many objects are eligible for garbage collection?
1. c1 is never referenced anywhere after C c2=m1(c1) and it is ready to gced. 2. C c2=m1(c1); m1 method results to a new C object whose reference c2 is replaced by c3. c2=c3; the object resulted from m1(c1) is also ready to be gced.
Originally posted by sweety sinha: this question is from examguide.com class C { public static void main(String a[]) { C c1=new C(); C c2=m1(c1); C c3=new C(); c2=c3; //6 anothermethod(); } static C m1(C ob1){ ob1 =new C(); return ob1; } } After line 6, how many objects are eligible for garbage collection?
the answer given is 2. how???
Hi,
This is because Objects which are going to be garbage collected when there are no more reference are there. So, here first c1,c2,c3 3 objects will be created. Now, at line C c2=m1(c1); when executed then c1 reference will be assigned to the obj1 so c1 is now free and it has no reference now.
Then, at line c2=c3 reference of c3 is assigned to c2 so now c3 is also free.
So, at this position there will be 2 objects c1 and c3 are eligible for garbage collection. So, the answer is 2
If all of you remember, objects are not passed as references of C++ but pointers of C++.
So c1 is not going to be garbage collected...
M Srilatha
Ranch Hand
Joined: Aug 27, 2008
Posts: 137
posted
0
I agree with Ankit.
only the object created inside the method m1 will be available for GC. c1 is not assigned a null value so still the object created at line 1 is referenced by c1. Hence, it is not eligible for garbage collection.
So the answer should be 1.
Thanks,<br />Srilatha M
sweety sinha
Ranch Hand
Joined: Jul 07, 2008
Posts: 76
posted
0
only the object created inside the method m1 will be available for GC.
The object referenced by c2 will be eligible for garbage collection
that is the object created as
C c2 = m1(c1);
m1 method will return a new object of class C. That object will be eligible for garbage collection at line 6. [ September 25, 2008: Message edited by: Ankit Garg ]
Lakshman Arun
Greenhorn
Joined: Sep 24, 2008
Posts: 19
posted
0
To be more precise, the object referenced by c2 which was created inside the method m1, is the only object available for GC
Paul Somnath
Ranch Hand
Joined: May 19, 2008
Posts: 177
posted
0
What I want to know is after the method m1 returns, does the reference pointed by ob1 would no longer be there?
If this is the case, then yes, the object created inside method m1 would only have a single reference (c2) and that also changes in line 6, where c2 refers to the object referred by c3.
Originally posted by sweety sinha: this question is from examguide.com class C { public static void main(String a[]) { C c1=new C(); C c2=m1(c1); C c3=new C(); c2=c3; //6 anothermethod(); } static C m1(C ob1){ ob1 =new C(); return ob1; } } After line 6, how many objects are eligible for garbage collection?
the answer given is 2. how???
Manju Kavi
Ranch Hand
Joined: Sep 25, 2008
Posts: 33
posted
0
I think only 1 object is eligible after line 6. that is that object created in m1 method. As we know all local variables die after method ends, so ob1 will be lost. And at line 6 c2 will refer to c3..