Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finalize and System.gc()

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..?
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jaydeep Mazumdar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic