• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

one question about gc?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here is a question from Khalid and Rolf book (pp255):
I am reading the answer in the book, even that I just did not get it. can anybody help me?
8.1 Select all the true statements:
1. An object will be gc immediately after the last reference to the object is removed.
2. if an object 1 is accessible from an object 2 and object 2 is accesible from the object 1, then both are not eligible for gc.
3. Once an object has become eligible fro gc, it will remain in this statusuntil it's destroyed.
4. if an object obj1 can access obj2 that is eligible for gc, the obj1 is also eligible for gc.
Can anybody comment on this topic?
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the full question (in my printing) is:


Which of these statements is true?
Select all valid answers.
(a) Objects can explicitly be destroyed using the keyword delete.
(b) An object will be garbage collected immediately after the last reference to the object is removed.
(c) If object obj1 is accessible from object obj2 and obj2 is accessible from obj2, then obj1 and obj2 are not eligible for garbage collection.
(d) Once an object has become eligible for garbage collection, it will remain eligible until it is destroyed.
(e) If an object obj1 can access an object obj2 that is eligible for garbage collection, then obj1 is eligible for garbage collection.


Comments:
(a) false. There is no delete in Java. Garbage collection can never be forced, only suggested and it is through a different mechanism. (System.gc() or Runtime.getRuntime().gc()).
(b) false. There is no way to tell when an object will be garbage collected...the garbage collector Thread is a low priority Thread (maybe it should unionize?)
(c) false. The key is that the active program/thread/method has to not have a reference to an object. Within any object of even minimal complexity there are tons of active references to other objects within in it.
Circular references don't matter...if they did then outer/inner class pairs couldn't be garbage collected (because an inner class has a reference to its enclosing class and if instantiated an outer class has a reference to its inner class). Another example of a circular reference is a Frame and its spawned Dialog.
(d) false. The object's finalize method which is called just before it is garbage collected could resurrect it.
(e) true. This is the simple case of an object with an instance member that is an object. Like,
public class Obj1 {
Obj2 obj2;

Obj1() {
obj2 = new Obj2();
}
// other methods
}
Now if somewhere in Obj1's methods, obj2 where set to null then the object it referred to would be eligible for garbage collection and an object obj1 still referred to elsewhere in the active program wouldn't be eligible for garbage collection but the statement stipulates that obj1 can reach obj2 so this isn't the case.
Instead, this is merely the case of an object, obj1, that is eligible for garbarge collection which has member variables, like obj2, that are active object references looked at from the point of view of those objects referred to. They are eligible for garbage collection therefore it must follow that the object that refers to them must be eligible for garbage collection as well or the reference wouldn't be active ones.
Did I make this better or worse?
Best regards,
Steve Butcher
exceptionraised@aol.com
[This message has been edited by sgwbutcher (edited June 28, 2000).]
[This message has been edited by sgwbutcher (edited June 28, 2000).]
 
Jason
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Steve,
Thanks alot, i got your point.
a little bit tricky!!
 
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic