Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
pallavi utukuri
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pallavi utukuri
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i got what u explained..........
if o1=null now 2 objects will b EGC ryt?
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks soo much at last i could stack this into my mind
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic