Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
vishesh gupta
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear all,
i got this statement in mughal book.
If object obj1 can access object obj2 that is eligible for garbage collection, then obj1 is also eligible for garbage collection.


kindly explain the above statement
thanks in advance
vishesh
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If obj2 is eligible for garbage collection then it has no live references to it. So if obj1 refers to obj2 then there can be no live references to obj1 either. Because if there was a live reference to obj1 there would be a live reference to obj2 (via the reference obj1 -> obj2). If obj1 has no live references to it then it is also eligible for garbage collection.

Logic - dear Watson! (I think Mr Holmes said that)
 
mmg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what they are referring to are the so-called "islands of isolation".

Say you have the following class:

class Person {
public Person partner;
}

and the following code:

public static void main(String[] args) {
1. Person p1 = new Person();
2. Person p2 = new Person();
3. p1.partner = p2;
4. p2 = null;
5. p1 = null;
}

After line 4 executes, you might think p2 can be garbage collected. However, p1 still has a reference to p2 and p1 can still be accessed so p2 can't be garbage collected. After line 5 exectues, there are no more references to p1 in the system (or more precisely: there are no more threads with a reference to p1). There still is a reference to p2 (coming from p1). So since p1 is definately egligible for garbage collection, p2 is also egligible for garbage collection (and so are all the other objects which ONLY p1 holds a reference to).
 
Rajah Nagur
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vishesh gupta:
dear all,
i got this statement in mughal book.
If object obj1 can access object obj2 that is eligible for garbage collection, then obj1 is also eligible for garbage collection.


kindly explain the above statement
thanks in advance
vishesh


When is any object in Java is eligible for GC? All objects that are not accessible from root.

Based on this --> obj2 is eligible for GC meaning it is not reachable by any objects from root. It has to be cleaned. Then the algorithm tries to find any objects that have only refernce to obj2 (in this case obj1) which also become eligible for GC. If obj1 was accessible from root then obj2 would never be eligible for GC in the first case.

Btw The root here includes:
1 object references in the local variables
2 operand stack of any stack frame and any object references in any class variables
3 object references that were passed to native methods that either haven't been "released" by the native method.

etc...

Hope this helps
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic