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
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.
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?!
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.
Joined: Aug 26, 2009
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.
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?!
Joined: Aug 26, 2009
Check out these links below. They will give you both overview and details of GC.