Page 255 question 8.1, Khalid & Rasmussen. e. If an object obj1 can access an object obj2 that is eligible for garbage collection, then obj1 is also eligible for garbage collection. Can anyone explain with a few lines of code. Thanks.
If an object obj1 can access an object obj2 that is eligible for garbage collection, then obj1 is also eligible for garbage collection. eram, I have responded your previous post regarding GC. Please go through that. I think the author means this.
So the above statement is false regds maha anna
[This message has been edited by maha anna (edited March 18, 2000).]
No, the statement is true. It's not talking about what happens if you set obj1's reference to obj2 to null. It's saying that assuming obj1 still has a reference to obj2, then the only way obj2 could be eligible for garbage collection is if obj1 is also eligible for collection. The fact that obj2 still has a reference from obj1 does not prevent collection if obj1 is not itself reachable from any active threads. Example:
After the last line, both Vectors are eligible for GC, even though they both still have references to each other - because neither Vector can be reached by any references in the code.
before i begin i would just like to say... "great example Jim!" well anyway i saw the title of eram's post and i think our question fits right in... so here it goes... on page 255 of Khalid's and Rolf's book under the "Invoking Garbage Collection" section it is stated that: "The System.gc() method can be used to force garbage collection..." i have seen a couple of mock exams say that garbage collection can never be forced... we're a bit confused here.. help of any kind would be very much appreciated! thanks!
System.gc() cannot force the garbage collector to run it merely requests that the garbage collector runs. I'm also under the impression that even though the gc runs and objects are eligible for garbage collection, there is no guarantee that the objects will be garbage collected - the gc thread could be interrupted by a higher priority thread and stop garbage collecting. Den