File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes GC doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "GC doubt" Watch "GC doubt" New topic
Author

GC doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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 ?

    Tell the difficulties that i am difficult.
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18996
        
      40

    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


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Greg Charles
    Sheriff

    Joined: Oct 01, 2001
    Posts: 2861
        
      11

    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

    Joined: Feb 09, 2012
    Posts: 583

    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
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18996
        
      40

    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

    Joined: Feb 09, 2012
    Posts: 583

    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
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18996
        
      40

    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
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: GC doubt