• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One more time :) G. C. Q and A

 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Reader,
A fellow Javarancher asked me to explain the answer to the following Garbage Collector question:

[ Jess added some carriage returns to break up the page a bit ]
[ January 10, 2003: Message edited by: Jessica Sant ]
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GREAT HELP
thanks
[ January 09, 2003: Message edited by: Melliholic Michael ]
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how does what you did on line 2 relate to the '.x' after the object names on the heap?
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i mean is,
if line 2 were
2. public X2 yo;
after lines 4 and 5, would the objects on the heap be x2.yo and x3.yo (which are really 2 X2 objects)?
[ January 10, 2003: Message edited by: Jasper Vader ]
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ January 10, 2003: Message edited by: Melliholic Michael ]
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x2.yo and x3.yo are the attributes of objects x2 and x3 respectively. Furthermore, x2.yo and x3.yo reference to Object x3 and Object x2 respectively
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Melli,
so on line 2, -- X2 yo -- would refer to the attributes of the X2 object?
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok - it's hard to draw pictures with characters :roll:
The little rectangles inside the objects are reference variables... so the objects themselves aren't really named (i call them line 4 and line 5 object).
x2.x and x3.x are the reference variables that come automatically as the instance variable of an X2 object whenever one is created.
I know the class names and reference variable names are terrible - in real life you'd be fired for writing code like this, but it's really close to what you'll find on the exam...
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm so is line2 a constructor without parentheses?
slow basic question i know but i am trying to figure out the code
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jasper -
line 2 is an instance variable! every time an X2 object is created, it gets an instance variable called x. It just so happens that this instance variable isn't an 'int' or a 'long' - it's a reference variable! it happens to be a reference variable of type X2 - so that one X2 object can refer to another X2 object...
does that help?
-Bert
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Bert, that helps
so i am wondering how the object x2 could reference object x3 using the reference variable of x?
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually, here's a question more On-Topic... i have been doing John Hunt's mick exam, and came across this GC question:

Which is the earliest statement, where the object originally held in e, may be garbage collected:
public class Test {
public static void main (String args []) {
Employee e = new Employee("Bob", 48);
e.calculatePay();
System.out.println(e.printDetails());
e = null;
e = new Employee("Denise", 36);
e.calculatePay();
System.out.println(e.printDetails());
}
}

a) Line 10
b) Line 11
c) Line 7
d) Line 8
e) Never
now i thought it would be line 6, where e is assigned null?? but that option is not given in the answers
is e initially a reference to the class Employee, rather than to any object?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is after the statement on line 6, i.e. at line 7, that the first Employee can be garbage collected.
- Peter
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jasper -
From the original code example at the beginning of this thread, look at line 6:
x2.x = x3;
What this line says is: 'The object referred to by x2 has a reference variable x ('x2.x'). Set this reference variable (x2.x) equal to the reference variable x3, which refers to the second X2 object (the one created in line 5). We know the following about line 6:
As far as line 6 is concerned, there is one object and there are two reference variables.
So, as of that moment there are two ways to refer to that one object:
x3 or x2.x // they both refer to the same object !!!
I know this is tricky, and you wouldn't really write code like this, but this really is how the exam questions will be.
When confronted with such a question, I ALWAYS draw a little picture, with references variables pointing (ok 'referring' ), to objects. I think this is the surest way to keep track of who's referring to whom, and which objects have how many references left. (Of course once you've drawn a line you'll have to cross it out or erase it, because they're always redirecting reference varaibles on the exam.)
Jasper - does this help?
-Bert
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Owwww yeahr! Bartender, slide tha man a coupla whiskeys down the bar!
I'm getting there Bert, thanks! the two references to the object sheds some light indeed.
but the '.x' way of referring to it is only possible because of the intial declaration of x being a reference variable of class X2 in line 2 yeah?
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter den Haan: (referring to the additional code i posted)
It is after the statement on line 6, i.e. at line 7, that the first Employee can be garbage collected.
- Peter

aha, thankyou for clearing up that point about the additional code example i posted earlier!! AFTER the line has assigned null, therefore on the next line, i see i see
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bert Bates:
1. class X2 {
2. public X2 x;
3. public static void main(String [] args) {
4. X2 x2 = new X2();
5. X2 x3 = new X2();
6. x2.x = x3;
7. x3.x = x2;
8. x2 = new X2();
9. x3 = x2;
10. doComplexStuff();
11. }
12. }

look at line 6:
x2.x = x3;
What this line says is: 'The object referred to by x2 has a reference variable x ('x2.x'). Set this reference variable (x2.x) equal to the reference variable x3, which refers to the second X2 object (the one created in line 5).

hmm - i would surmise that the first X2 object is still alive so far, even tho x2.x refers to the second X2 object? Because what does x2 refer to at this stage?
and on line 7, that first object is referred to by x3.x = x2?

We know the following about line 6:
As far as line 6 is concerned, there is one object and there are two reference variables.
So, as of that moment there are two ways to refer to that one object:
x3 or x2.x // they both refer to the same object !!!

but then after line 7, line 8 says that the object initally referred to by x2 has gone, in the sense that x2 now refers to a new X2 object.
so one onject is available for g.c. here? and then line 9 says that the object referred to by x3 is now g.c. bait, because x3 now refers to a new X2 object?
so two objects are available for g.c.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jasper -
We've almost got these objects rounded up! Your last response was really close - here's just a little modification...
After line 5, two objects have been created - each with its own reference variable. After line 7, each object now has two ref. vars. referring to it, for a total of 4 ref. vars. !!
After line 8 executes, only 1 of the four ref. vars. has been redirected, you can still get to the line 4 object via x3.x. So nothing is GC bait yet... but line 9 runs and redirects x3 to the new line 8 object. So at this point there is a new object (created on line 8) with two ref. vars. The objects created on lines 4 and 5 still exist, (probably, the GC isn't THAT fast), and those two objects refer to each other, but how can YOU refer to them?
Since you can't refer to them, they are GCable !!
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. class X2 {
2. public X2 x;
3. public static void main(String [] args) {
4. X2 x2 = new X2();
\\first object referred to by x2
5. X2 x3 = new X2();
\\second object referred to by x3
6. x2.x = x3;
\\second object is now referred to by x2.x
7. x3.x = x2;
\\first object is now referred to by x3.x
8. x2 = new X2();
\\third object referred to by x2. first object is
\\still referred to by x3.x
9. x3 = x2;
\\ x3 now refers to the third object x2. I guess this is the main reason that two objects are now available for G.C.?
\\but what happens to x3.x after this assignment?
10. doComplexStuff();
11. }
12. }

so x3 refers to the third X2 object. x2 refers to the third X2 object.
Originally posted by Bert Bates:
Jasper -
The objects created on lines 4 and 5 still exist, (probably, the GC isn't THAT fast), and those two objects refer to each other, but how can YOU refer to them?

What i am unsure about is: how do we know what x2.x and x3.x refer to? When a new X2 object is created, does it instantly 'take over' the reference of x2.x for itself?
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jasper -
I really think THE BEST WAY to understand this kind of problem is to draw pictures, like at the beginning of this thread. So we use the following conventions:
The stack is a column on the left.
The heap is a column on the right.
Reference variables are drawn as small squares
Objects are drawn as kind of blobby circles
USUALLY - reference variables live on the stack, and ALWAYS objects live on the heap. The exception to this rule is that if a reference variable (ref. var.) is an 'instance variable' (line 2 is an example), then that ref. var. lives in the object, on the heap!
So those are the rules! Now you can go back to the diagram and see if it makes sense.
So everytime you make a new instance of class X2, it comes with an instance variable called 'x' built-in! (no charge!). It just so happens that this instance variable happens (blind luck ) to be a ref. var. - capable of referring to an object of type X2. So, IF YOU WANT TO (it's optional), you can explicitly refer that built-in ref. var. to an X2 object. If you do nothing with it, it is null. EVERYTIME you make a new X2 object, you get this new ref. var. which you can use if you want to.
Now go back to the diagrams at the top of the thread... in the last picture, the new object (line 8) is at the top: two ref. vars, that live on the stack, are referring to it, and it has its built-in ref var (the box in the circle), but it doesn't refer to anything - at this point it's null.
Lower in that picture are the two 'old' X2 objects. Their built-in ref. vars. are not null, they each refer to the other one's X2 object. (that's what the arrows on the sides indicate).
That didn't happen automatically, we had to explicitly assign them in lines 6 and 7.
Does that help?
-Bert
p.s. I really, really recommend that you take a pencil and paper and draw those pictures yourself as you follow the code. It's just too much to keep track of in your head, and I guarantee that there WILL BE questions just like this on the exam!
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey! I finally get it! There are two objects available for garbage collection!

nice one Bert. Good idea about the drawing pictures approach. I'll always use that from now on. Using the Stack and the Heap rectangles, I can totally see how at line nine those first two objects are just left stranded pointing at each other, but nothing pointing at them. Spells G.C. to me!
 
Milan Romasini
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I understand posts so far.
I'd like to ask how does garbage collection work in a similar situation?
Lets say we have code:
public class One {
protected One() {
}
static private One _instance = null;
static public One instance() {
if(null == _instance) {
_instance = new One();
}
return _instance;
}
}
How can an unique instance of this class be 'reused'? Won't it be garbage collected just after the first invocation?

Thanks, Milan
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a guess:
although it has a value of null, the reference "_instance" still refers to it, so it is not G.C ready yet.
I guess if _instance was referred to another instance, the initial object would be G.C.'d.
 
Milan Romasini
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After the first invocation, with One.getInstance(), _instance is not null anymore. The first invocation will create an instance of One object and store its reference in _instance.
I think it's not garbage collected because it has static member and this member can be referenced with One.getInstance()
Milan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic