• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

Funadamental doubt on garbage collection

 
Ranch Hand
Posts: 63
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello & Hi to all the ranchers....

Here i have small doubts about garbage collection, can be understood with the following two classes.



Here, if we made the object "gc" null then the finalize() is being executed. does it really mean that "Memory allocated for the object 'gc' on heap reclaimed ?"

in the other case, if "gc" not made null the finalize is not executed. "May i assume it as 'The memory allocated to gc not reclaimed even after the completion of the

main method' ".

And another confusing thing is "Will the finalize() surely executed before the object's deallocation ?"



Waiting for your answers.........





 
lowercase baba
Posts: 12994
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding:

"System.gc()" doesn't really do anything. it SUGGEST to the JVM that now might be a good time to do GC. However, the JVM is free to ignore your suggestion. The JVM should know about how much memory it has free. If it still has 90% available, and you say "DO A GC NOW!!!", the smart money would be on it saying "Meh - why waste my time?".

So, in this program, you have no guarantee that a GC ever happens.

In your first example, when you set gc = null, that object is ELIGIBLE for collection, but if the garbage collection never happens, it won't be.

When the main method ends, the JVM shuts down, and ALL that memory is returned to the OS. There may be some strange cases where a thread can continue to run after main() ends, but that is not the case here.
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okey, let me try to make it little more clear:

"Memory allocated for the object 'gc' on heap reclaimed ?"



If "finalize" got executed then you can be sure that the memory is deallocated.

in the other case, if "gc" not made null the finalize is not executed. "May i assume it as 'The memory allocated to gc not reclaimed even after the completion of the

main method' ".



You HAVE called System.gc() IN the main method itself so you assumption of "Completion" of main method is wrong. When the System.gc() was called the main method has not completed. And because you haven't made the reference as null, which results in an active reference to the object and hence it is ineligible for garbage collection.


"Will the finalize() surely executed before the object's deallocation ?"



YES, the finalize() will surely be called before an object is deallocated but there is NO SURETY that an object will be deallocated on calling of System.gc() as stated by Fred.

HTH.

reply
    Bookmark Topic Watch Topic
  • New Topic