aspose 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

vishesh gupta
Greenhorn

Joined: Jun 30, 2006
Posts: 1
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

Joined: Aug 03, 2002
Posts: 7729
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)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
mmg
Greenhorn

Joined: May 10, 2006
Posts: 11
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

Joined: Nov 06, 2002
Posts: 239
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


You can't wake a person who is <b><i>pretending</i></b> to be asleep.<br />Like what <b>"it"</b> does not like - <i> Gurdjieff </i>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection