File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collector 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 collector" Watch "garbage collector" New topic
Author

garbage collector

umang bhartia
Ranch Hand

Joined: Sep 29, 2000
Posts: 60
hi every 1,
i m unable to understand how GC works, can any one explain me this with example. I want to know as to when an object get GC.
thank u
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Some points that I have about GC are:
1. You cannot force GC reliably. You can make things eligble for garbage collection, but you can't force it to be collected.
2. You can make an object eligble for garbage collection by assigning all references to it to null. Remember though, if you create an object and then assign that object to another object, then you have 2 references. It won't be eligble for GC unless both are set to null or go out of scope.
3. When a reference variable to an object goes out of scope and is no longer accessible, then it is also eligble for GC, again assuming there are no other refereces to the object that are still in scope.
Those have been the main points that I have run across when dealing with GC and the exam. People feel free to correct me if I missed some or stated something wrong.
Bill
Ayman Jaffar
Ranch Hand

Joined: Sep 06, 2000
Posts: 57
You CAN'T say when an object is garbage collected. The time an object is garbage collected depends on the implementer(creater) of the Java Virtual Machine (JVM), which is responsoble for garbage collection.
But you can always PREDICT that an object is ELEGIBLE for garbage collection when there are no refrences pointing to that object.
an example would be :
1 class GarbageCollectionExample {
2 public static void main (String[] args) {
3 String s = "Hi";
4 s = "bye";
5 }
6 }
In this case, the object "Hi" is ELEGIBLE for galbage collection at line 4, becuase the variable "s" now points to another object which is "bye". But although it's ELEGIBLE it doesn't mean that this object will be garbage collected at line 4. It could be garbage collected at line 5 or 6. Or the program could execute and it wouldn't be garbage collected, if the latter happens then usually it's the Operiting System that does the garbage collection for this "Hi" object.
The theme of the story is, you can never say when an object is garbage collected. You can only say on object is ELEGIBLE for being garbage collected !!
Hopes this helps !


america
umang bhartia
Ranch Hand

Joined: Sep 29, 2000
Posts: 60
Thanks both of u,
now i m some what clear as to when an object is elegible for GC.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collector
 
Similar Threads
Doubt Garbage Collection
Doubt in Garbage Collection (GC) question in K&B Book.
Java joke
Garbage collection
need help with garbage collector