• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC doubt

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • 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 ?
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 20885
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • 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
     
    Greg Charles
    Sheriff
    Posts: 2984
    12
    Firefox Browser IntelliJ IDE Java Mac Ruby
    • Mark post as helpful
    • send pies
    • 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
    • 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
    Marshal
    Pie
    Posts: 20885
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • 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
    • 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
    Marshal
    Pie
    Posts: 20885
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • 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
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic