• 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

GC doubt

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
KB 6

Two-Minute Drill


Garbage Collection (Objective 7.4)
  • The finalize() method is guaranteed to run once and only once before the garbage collector deletes an object.


  • My doubt is that if my class in unloaded.After that JVM finds any need to delete an object of that class then how it is guaranteed to run finalize method once ?
     
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    saloni jhanwar wrote:
    My doubt is that if my class in unloaded.After that JVM finds any need to delete an object of that class then how it is guaranteed to run finalize method once ?



    The class loader can't unload any classes which there are instances still in existence. And for the default class loader, the one for stand alone programs, I believe it is configured to never unload a class.

    Henry
     
    Sheriff
    Posts: 3063
    12
    Mac IntelliJ IDE Python VI Editor Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    They mean the finalizer will run once per instance, not per class. I can't think of a scenario that would cause the class to be unloaded before the garbage collector finished destroying the instances of that class. Is that what you're asking?
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Greg Charles wrote:They mean the finalizer will run once per instance, not per class. I can't think of a scenario that would cause the class to be unloaded before the garbage collector finished destroying the instances of that class. Is that what you're asking?


    I read like that we don't know when JVM will delete eligible objects.When JVM will find any need to delete eligible object like on low memory then it will delete.I am not getting exact meaning of that line.Does that mean JVM will surely delete all eligible objects of class just before program exit ? or even after program exit there will be eligible objects of that class for GC.
     
    Henry Wong
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    saloni jhanwar wrote:
    I read like that we don't know when JVM will delete eligible objects.When JVM will find any need to delete eligible object like on low memory then it will delete.I am not getting exact meaning of that line.Does that mean JVM will surely delete all eligible objects of class just before program exit ?



    No. The standard Oracle JVM only does a GC when it needs memory. It doesn't need memory to exit. It just exits.

    saloni jhanwar wrote:
    or even after program exit there will be eligible objects of that class for GC.



    There is no such a thing as eligible objects after the JVM exits. There is no such a thing as classes after the JVM exits. It is just a bunch of resources, including memory resources, that the operation system will reclaim when the program (in this case, the JVM) terminates.

    Henry
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Henry Wong wrote:

    saloni jhanwar wrote:
    I read like that we don't know when JVM will delete eligible objects.When JVM will find any need to delete eligible object like on low memory then it will delete.I am not getting exact meaning of that line.Does that mean JVM will surely delete all eligible objects of class just before program exit ?



    No. The standard Oracle JVM only does a GC when it needs memory. It doesn't need memory to exit. It just exits.

    saloni jhanwar wrote:
    or even after program exit there will be eligible objects of that class for GC.



    There is no such a thing as eligible objects after the JVM exits. There is no such a thing as classes after the JVM exits. It is just a bunch of resources, including memory resources, that the operation system will reclaim when the program (in this case, the JVM) terminates.

    Henry



    Sorry, it was not about JVM exit.I were asking that when any java program does terminate then just before its termination, all eligible objects of that program would be surely collected by GC ? or even after termination of that program there might be eligible objects for GC.
     
    Henry Wong
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    saloni jhanwar wrote:
    Sorry, it was not about JVM exit.I were asking that when any java program does end after execution then just before its end, all eligible objects of that program would be surely collected by GC ? or even after end of that program there might be eligible objects for GC.



    When the last of your program is done, but before the JVM exits, there could be lots of objects that are eligible for GC, that will *not* be collected..... and why "surely" must the GC collect the garbage? The JVM is going to exit anyway.

    Henry
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic