• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finalize and System.gc()

 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..?
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic