• 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

garbage collection

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java has automatic garbage collector.we can define finalize()method that will be run by the garbage collector.if we explicitely define system.gc()in a program then can the same program have finalize() method?if yes, then how will the code written in the finalize()be executed?by whom?and how to transfer the program flow to the finalize()method?
if the program can contain the finalize()method and also System.gc()then what happens if System.gc()gets executed beforew the finalize()method?
thank you.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method System.gc() tells the JVM that running the garbage collector now might be a good idea. It's a suggestion, not a command, and the JVM can ignore it.
Before an object is garbage collected, the collector will call finalize() on that one object, yes. There's no guarantee, though, that any given object will ever be collected, you realize.
You can't redefine the System.gc() method, so most of your question doesn't make any sense.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To take Ernest's discussion a little further, because the GC runs unpredictably using the finalize() method is not generally recommended. It is there, and IF the GC runs, it will call an object's finalize() method before destroying the object, but it's too erratic to rely on for anything important.
 
mrudul joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much for clearing my concept!i am preparing for java programmer certificcation and a lot of such small things that require basics haunt my mind . I am sorry to ask such a basic question.
thanks again
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic