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 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" Watch "garbage collection" New topic
Author

garbage collection

pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
This question is from whizlabs
how many objects are eligible for garbage collection after the execution of line 8?
class gc{
public static void main(String args[]){
Object obj=new Object();
Object o1=foo(obj);
Object o2=new Object(); .. line 7
o2=o1; // line 8
//more code
}
public static Object foo(Object o){
o=new Object();
return o;
}
}
the answer is 1 explaining that o2=o1 makes o2 refer to the object referred by o1 hence the prev object created at line 7, which was being referred by o2 earlier becomes eligible for garbage collection
can anyone plz explain this....
thanks


Thanks,<br />Pallavi
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
The answer is very clear and straight forward. The object created at line 7 is referenced by 'o2', now this object looses it reference at line 8.
This is will explain it more clearly
O1 ---> [Object 1] //line 6
O2 ---> [Object 2] //line 7
At line 8
02 -/-> [Object 2]
O2 ---> [Object 1] <--- O1
As you can see 'Object 2' has lost its reference therefore it EGC.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
if i got what u explained..........
if o1=null now 2 objects will b EGC ryt?
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by pallavi utukuri:
if i got what u explained..........
if o1=null now 2 objects will b EGC ryt?

Well not exactly, as you can see
O2 ---> [Object 1] <--- O1
'[Object 1]' has two references now, it will be EGC iff both of them are assigned to null. That is O1 ---> null and O2 ---> null
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
k sooo
Object obj=new Object();
Object o1=foo(obj);
Object o2=new Object();
o2=o1;
o1=null;
o2=null;
soo to sum it up:
1. original obj referenced by o2
2. o1=null; 01 is not egc as 02 has ref of o1
3. 02=null; the ref of o2 which is o1 is egc
4. as o2 now does not have any ref it is also ego
which makes 3 egc ryt?
kindly bear with me when it comes to gc ys i am very dymb
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
pallavi, you are confused about the difference between a 'referece' and an 'object'.
obj, o1, o2 are all references (They are NOT objects) -- They can't be garbage collected.
[Object1], [Object2] and [Object3] in the following example are objects -- They can be garbage collected
References are kept in the stack memory while objects are kept in the heap.
Here is what happens in your example...

Object obj=new Object();
obj ---> [Object1]
Object o1=foo(obj);
o1 ---> [Object2]
Object o2=new Object();
02 ---> [Object3]
o2=o1; // By the time this code gets executed [Object3] is EGC
o1 ---> [Object2] <--- o2
o1=null;
o1 -/--> [Object2] <--- o2
o2=null;
o1 -/--> [Object2] <--/- o2 // By the time this code gets executed [Object2] is EGC

As you can see there is two objects EGC (object3 and object2), object1 on the other hand is not garbage collected because it is still being referenced by 'obj'.
pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
hey thanks soo much at last i could stack this into my mind
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection