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 Garbage Collection clarification: 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 "Garbage Collection clarification:" Watch "Garbage Collection clarification:" New topic
Author

Garbage Collection clarification:

Fred Roger
Greenhorn

Joined: Nov 01, 2006
Posts: 19
Hi guys,

Please clarify me:

class I {
private String name;
public I(String s) {name = s;}
private I other;
public void other(I i) {other = i;}
}

class J {
private I i1 = new I("A"), i2 = new I("B"), i3 = new I("C");
private void m1() {
i1.other(i2); i2.other(i1); i3.other(i3);
i1 = i3; i2 = i3; // line 1
//here
}

public static void main (String[] args) {
new J().m1();
}}

According to my understanding:

We have three objects say 1, 2, 3

before line 1:

1 ----> referred by i1 and i2.other
2 ----> referred by i2 and i1.other
3 ----> referred by i3 and i3.other

After line 1:

As i1 and i2 will be refering to 3(3rd object)
3-----> i1, i2, i3, i3.other

Question is: What will happen to the refernce named i1.other, i2 other.

Will they also refered to 3 now ??

Or they will continue to refering to theie old objects ??

In first scenerio there will be 2 object for GC at //here
or According to second scenerio there will be no objects GC at // here.

Please guy correct me if I am wrong anywhere.

Thanks a lot
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
Originally posted by Fred Roger:
Question is: What will happen to the refernce named i1.other, i2 other.
Will they also refered to 3 now ?? Or they will continue to refering to theie old objects ??

The latter (second scenario).
According to second scenerio there will be no objects GC at // here.

Both 1 & 2 will be eligible for GC after "// here" -- an object is eligible for garbage collection if it cannot be accessed by any live thread (and that can be the case even if an object still has references pointing at it). This is the situations that K&B call "islands of isolation", if you have that book; see page 249.
[ November 07, 2006: Message edited by: Matt Russell ]

Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
Dave McIntyre
Greenhorn

Joined: Oct 22, 2006
Posts: 8
Originally posted by Fred Roger:
Hi guys,

<snip>
According to my understanding:

We have three objects say 1, 2, 3

before line 1:

1 ----> referred by i1 and i2.other
2 ----> referred by i2 and i1.other
3 ----> referred by i3 and i3.other

After line 1:

As i1 and i2 will be refering to 3(3rd object)
3-----> i1, i2, i3, i3.other

Question is: What will happen to the refernce named i1.other, i2 other.

Will they also refered to 3 now ??


It may help your understanding if you avoid thinking of the reference variables as i1.other and i2.other. We have six reference variables: three nice easy-to-understand reference variables i1, i2 and i3 on the stack and three more sitting inside the three objects 1, 2 and 3 on the heap. So it might be better to think of these three as being 1.other, 2.other and 3.other, because the references they hold don't change when the references that i1, i2 and i3 hold get changed.

Once i1 and i2 have been made to point to object 3, the reference variable 1.other is pointing to object 2 and the reference variable 2.other is pointing to object 1. But since there are no stack variables pointing to either object 1 or object 2 they have become unreachable Islands of Isolation and so they are eligible for garbage collection.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

after the line
private I i1 = new I("A"), i2 = new I("B"), i3 = new I("C");
we have this:

the other variables are still unassigned.

Then after line
i1.other(i2); i2.other(i1); i3.other(i3);
gives --->

i3.other points to its own I i3object. Can we say "mother object"? Don't know.

i2's and i1's other point to each others i object.



And after line
i1 = i3; i2 = i3; // line 1
//here

we have


The upper two objects form an island of isolation (dotted line) and are eligible.
The String objects they own are also eligible, so there will be
four objects for the garbage collector.


Yours,
Bu.


all events occur in real time
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Nice explanation Bu.
One more question?
When // doStuff is reached, how many objects are eligible for GC?


~Sanjeev Singh<br />SCJP 1.5
Fred Roger
Greenhorn

Joined: Nov 01, 2006
Posts: 19
Thanks to Burkhard and Dave for wonderful explanation.

Thanks again.
Valentin Mone
Greenhorn

Joined: Nov 05, 2006
Posts: 24
Originally posted by Sanjeev Kumar Singh:
Nice explanation Bu.
One more question?
When // doStuff is reached, how many objects are eligible for GC?


2 objects: c1 and c3. Although obj c2 is passed to the go method and set it's value to null, it will not change the actual object since c2 is just a copy of the reference.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi all,

wasn't this CardBoard thing in the K&B book?

the line
CardBoard c3 = c1.go(c2);

does nothing more than
Card board c3 = null;

because you cannot null a reference via a method parameter.
So basically we have
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = null;
c1 = null;
When we are here two objects are eligible, the first CardBoard and its Short that is an Object through autoboxing.

Yours,
Bu.

---
ticker: ... democrats took lancashire, la mancha, minas gerais and rajastan.... gop defends tasmania ...
Valentin Mone
Greenhorn

Joined: Nov 05, 2006
Posts: 24
Question:
Short x = 5; is created on heap?
I've heard something about some sort of "pool" if numbers are between -128 +127. Example:
Integer i1 = 10,
Integer i2 = 10

i1 == i2 -> true

Integer i3 = 128,
Integer i4 = 129,
i3==i4 -> false
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Valentin Mone posted November 08, 2006 08:05 AM
Question:
Short x = 5; is created on heap?


Yes, you are right, there is missing something.

I think, the Short object is on the heap only once for c1. As it is the first use of the Short object 5, a new Short has to be made on the heap, and a copy goes to the pool. I'm not sure.

If we nulled c2 after c1 (c2's variable "story" only points to the 5 in the pool), there would be no Short 5 on the heap left to be garbage collected.

Maybe start another thread about this?


Yours,
Bu.
scjp benice
Greenhorn

Joined: Nov 08, 2006
Posts: 1
Does compile error occurr? cauz m1 is private member of class J, therehy can only be accessed within the class, but not by the object instance "new J()".
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi, scjp!

sooner or later someone will turn up and tell you to change your name...

In the meanwhile,

Welcome to the Java ranch!



To your question:
No, the private method is accessed from inside the class. So it is visible.

Something else:
Here is a variation of the original code posted by Fred. It is modified slightly to give some output.


Output is system / JVM dependent, but can easily be:
finalizing the one with B
finalizing the one with A


Yours,
Bu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection clarification: