wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Finalize and System.gc() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finalize and System.gc()" Watch "Finalize and System.gc()" New topic
Author

Finalize and System.gc()

Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

When do we go for finalize() and when do we go for system.gc() exactly?
I've been wondering when i may encounter a situation to use them..can someone explain with examples..?


OCPJP 6
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Actually we never use them. When we do System.gc() we just request the JVM to start the garbage collector. When we call finalize it simply a normal method call as finalize is inherited from the Object class. But when the JVM actually does the garbage collection and removes an object then it will call its finalize method once before removing it. Always JVM will call the finalize method on an object once and this calling of finalize method by the JVM does not depend of how many times the user has called finalize method.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Jaydeep Mazumdar
Greenhorn

Joined: Aug 26, 2009
Posts: 23
The finalize() method of an object is called by the Garbage Collector before it removes the object from memory (when there are no references to that object).
You can write the object cleanup code in the finalize method. This is similar to a destructor in C++.

The System.gc() call is a request to the JVM to run the Garbage Collector. Note that it only *requests* the Garbage Collector to run.

Hope this helps!
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

(when there are no references to that object).

Not entirely true. Within the heap the objects may refer to other objects(islands of isolation). Well objects are garbage collected only when they are not referred by a live thread.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Actually the garbage collector keeps on running in the background,right?
Then why do we run System.gc() to start the garbage collector??
Truly speaking am not really clear about the garbage collector...doesnt it run continuosly in the back?!
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Actually whether to run the garbage collector or no its upto the JVM. Also i don't think it always runs in the background. System.gc() is just a request made by the User to the JVM, well then its upto the JVM to listen to it or no.
Jaydeep Mazumdar
Greenhorn

Joined: Aug 26, 2009
Posts: 23
Hi,

It is not necessary for the Garbage Collector to run concurrently. Infact, it is just one of the various algorithms available for grabage collection. There is a JVM option using which you can select the type of garbage collection you want to use.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

If the garbage collector is not continuously running in the background, then how come the references of variables that are no longer necessary(whose scope is over) gets removed automatically..?
isnt that why java is said to have automatic garbage collector??
so should we always call up the garbage collector in all our programs to make it much more efficient and perfect?!
Jaydeep Mazumdar
Greenhorn

Joined: Aug 26, 2009
Posts: 23
Check out these links below. They will give you both overview and details of GC.

Overview:
http://www.petefreitag.com/articles/gctuning/

Detailed Info:
http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finalize and System.gc()