File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I doubt my garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I doubt my garbage collection" Watch "I doubt my garbage collection" New topic
Author

I doubt my garbage collection

Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
How many objects are eligible for gc by the time the f() method is invoked?
I say there is only one eligible for gc: "a", since reference variable "a" has been assigned to object b and reference variable "b" has been assigned to object "c", which leaves the object originally referred to by reference variable "a" without a reference. The answer given to me is that the two objects orginally referred to by "a" and "b" are eligible for gc.

Whose correct here? If they're answer is correct could you please help me understand how it is that both objects originally referred to by "a" and "b" are eligible for gc?
Thank you
g


G
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
X a = new X();
X b = new X();
X c = new X();
a=b;//line 1
b=c;//line 2
f();

Here at line 1 a is now referring to b. At line 2 b is now referring to c. Both are now referring to c. So both are eligible for GC.


SCJP-1.5<br />SCWCD-1.4
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424

At line 2 b is now referring to c. Both are now referring to c

When an Object has more than one reference variable, reassigning one to refer to diff object doesnt reassign the other.
In the example only one object is eligible for GC.
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
Only 1 should be eligible for GC.
a is pointing to what b pointed
b is pointing to what c is pointing.


what is the correct answer.
Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
I agree with Ahmed and Lucky.... reference "a" changed to point to "b", and "b" shifted to point to "c". The result that see is the following:
1. object originally referred to by "a" has no reference, therefore
eligible for gc
2. object originally referred to by "b" is being reffered to by "a", so
there is a reference pointing to this object and therefore this
object is not elgible for gc
3. object originally referred to by "c" is being refrered to by "b"
and "c" so there are two references pointing to this object and
therefore this object is not eligible for gc

As I see it this leaves 1 (one) object eligible for gc.

Can another java guru please help?
thank you
g
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Can someone please explain me the difference here in following 2 scenarios?

2 separate situations are here for 3 objects a,b,c. Assume that all three objects are created.

1.

a=b;
b=c;

2.
a=b=c;

So finally in both assignment statements , how many objects will be eligible for GC?
Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
Dolly:

Why is this so confusing?

In a=b=c..... a holds the reference for b, which holds the reference for c... So both a and b are pointing to c (if I can use the word "point" as a matter of helping me understand the concept) Correct or no?
thanks
g
Gautam Pandey
Greenhorn

Joined: Sep 21, 2007
Posts: 21
The object which a refers to after the lines (meaning now a is pointing to b's object and both b and c are pointing to c's object) is not referenced by any variable or reference. c's not changed.
so i think only ONE object (originally referred to by a)is eligible. I don't see any ambiguity here.
[ September 24, 2007: Message edited by: Gautam Pandey ]
Vas Golla
Greenhorn

Joined: Aug 09, 2007
Posts: 8
Iam having a doubt that are a,b and c are references or instances?
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424

Here "a" is a reference variable of an instance of this object.
Sunil krishnatry
Greenhorn

Joined: Sep 25, 2007
Posts: 4
Only the object pointed by a will be eligible for garbage collection as this is the only object without reference.

Object b and c have are still reachable objects.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11295
    
  16

Can someone please explain me the difference here in following 2 scenarios?

I'll take a stab. assume in each case that originally a refers to 1, b refers to 2, and c refers to 3
1.

a=b;
b=c;

after the first line, a now refers to the same spot as b. so, both a and b refer to 2. c still refers to 3, and nothing refers to 1 anymore.

you run the second line. you change what b refers to, making it refer to the same thing as 3. so, a still refers to 2, b now refers to 3, and c refers to 3, with nothing referring to 1.


2.
a=b=c;

here, because of the associativity of the "=", you can think of this as if it were written

a=(b=c)

so here, first we change b to refer to c. so that would make a refer to 1, b and c both refer to 3, and nothing refers to 2. now, the value returned from the assignment is the thing being assigned, so "(b=c)" becomes, effectively, "c". this then effectivly gives you a=c.

so, now a refers to 3 as well, and nothing refers to 1 or 2.


So finally in both assignment statements , how many objects will be eligible for GC?

in the first example, 1 object. in the second, 2.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
Well it seems we have difference of opinion amongst us. Dolly maintains that there are 2 objects eligible for gc, while others, including myself, maintain that there is only 1 object eligible for gc.

FYI: the mock exam tesing software says that there are 2 objects eligible for gc, but I continue to doubt their answer.

Anybody else want to doubt with me or try to explain to me why there are 2 objects eligble for gc and not 1?
thanks again
g
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18563
    
    8

I think some less confusing explanations could be provided.

First of all, calling the three objects "a", "b", and "c" is confusing because that gets you mixed up with the names of the three variables that initially refer to them. So let's call them X1, X2, and X3.

Initially a -> X1, b -> X2, and c -> X3.

Then after a=b, a -> X2, b -> X2, and c -> X3.

Then after b=c, a -> X2, B -> X3, and c -> X3.

So there are references to X2 and X3, but no references to X1.
Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
OK! Well I don't know about the rest of you folks but for me I have to thank our bartender heroes for straigtening this mess out.

So now I see that my answer is correct as the code was structured thusly:

a=b;
b=c;

So this leaves only one object eligible for gc. Had the code stated:

a=b=c;

then I can see that there would be two eligible for gc.

thank you bartenders... I'll have another cup of java..
g
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Thank You bar tenders. I got it now.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy,

you can check for yourself the garbage collector if you override the finalize method an put some output inside.

Example:

Try it.

Yours,
Bu.


all events occur in real time
PK Ghosh
Greenhorn

Joined: Aug 08, 2006
Posts: 24
I tested the code for "a=b=c". Its 2 object garbage collected.

a points to b, hence a is gone
b points to c, hence b is gone and immidiately a is updated its reference to b (because b is alived through c) which is basically points to c.

hope this helps.
Gautam Pandey
Greenhorn

Joined: Sep 21, 2007
Posts: 21
Nice Code

Output is

main started
Y-a finalizing
main ready
Sean Nguyen
Greenhorn

Joined: Sep 26, 2007
Posts: 3
Hi,
Here is a little code to test both question:
a=b
b=c

and a=b=c

For the first case, 1 object is for GC. For the second case, 2 objects because (b=c is perform first, then a=b is perform later) the right side of the equal is perform first.




import junit.framework.TestCase;



public class Exame extends TestCase {
public void testGC(){
Xname a = new Xname("A");
Xname b = new Xname("B");
Xname c = new Xname("C");
a = b;
b = c;
System.out.println(a.getName() + b.getName() + c.getName());


a = new Xname("A");
b = new Xname("B");
c = new Xname("C");
a=b=c;

System.out.println(a.getName() + b.getName() + c.getName());
}


private class Xname {
private String name;
public Xname(String name){
this.name = name;
}
public String getName() {
return name;
}
}
}
Sean Nguyen
Greenhorn

Joined: Sep 26, 2007
Posts: 3
Forgot adding the reulst:

First case;
BCC

Second case:
CCC
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I doubt my garbage collection