• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I doubt my garbage collection

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iam having a doubt that are a,b and c are references or instances?
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here "a" is a reference variable of an instance of this object.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You bar tenders. I got it now.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice Code

Output is

main started
Y-a finalizing
main ready
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot adding the reulst:

First case;
BCC

Second case:
CCC
 
What's that smell? Hey, sniff this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic