• 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

why finalize() method is not called directly?

 
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) why should i always use Runtime.getRuntime().gc() , instead i an override finalize() method and call it directly(through respective instance) for collecting my de-referenced objects.

2) why there is 2 way of calling gc() method one through System class (System.gc() ) and through Runtime class
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calling finalize() will not cause anything to happen to the object except what is coded within finalize. Besides, if you have a reference to the object in order to be able to call finalize(), it's not eligible for garbage collection in the first place.

finalize() is called as a result of garbage collection, not to initiate it.
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Calling finalize() will not cause anything to happen to the object except what is coded within finalize. Besides, if you have a reference to the object in order to be able to call finalize(), it's not eligible for garbage collection in the first place.

finalize() is called as a result of garbage collection, not to initiate it.



How to come to know whether it's eligible or not ??? , for garbage collection.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An object is eligible for gc when there are no longer reference to it. Or when it is part of an island of isolation.
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you tring to say that finalize() method must not be called directly instead must be called through garbage Collection , i don't get IT!!
 
Ranch Hand
Posts: 37
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding is if the object is not referenced by any active thread the object will be eligible for Garbage. It’s decided by JVM when to start the garbage collection process. In the finalize() method we can make the reference of the object to null to make this object eligible for garbage collection. Seniors please correct if we are missing something.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun Giridharan wrote:Are you tring to say that finalize() method must not be called directly instead must be called through garbage Collection , i don't get IT!!


From the API docs

The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized


As you can see, finalize() is invoked by the JVM. You never need/should call it yourself. This method provides you a last chance opportunity to release resources such as file handles and streams, which you might have opened in your class.

One point to note about the gc() method. Invoking it does not mean the eligible objects will be reclaimed/garbage collected. That still remains the prerogative of the JVM. From the API docs

When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

As you can see, there is no guarantee. For this reason, it is always advisable to perform your cleanup operations in the code itself, instead of putting them in your finalize and relying on the JVM to invoke it.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shinil Mohan wrote:My understanding is if the object is not referenced by any active thread the object will be eligible for Garbage


collection.

Shinil Mohan wrote:It’s decided by JVM when to start the garbage collection process.


Generally correct.

Shinil Mohan wrote:In the finalize() method we can make the reference of the object to null


No, you can't.

Shinil Mohan wrote:to make this object eligible for garbage collection.


I believe the best way to make objects eligible for garbage collection is simply to allow the references to drop out of scope. For certain classes, it is also necessary to release native resources, usually via a dispose() or close() method. Rarely, you may have a class or instance field that you would like to release to GC, in which case setting the variable reference to null is the only way to do it.

Shinil Mohan wrote:Seniors please correct if we are missing something.


Ditto here.
 
Shinil Mohan
Ranch Hand
Posts: 37
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the details.....
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:

Shinil Mohan wrote:In the finalize() method we can make the reference of the object to null


No, you can't.



Ah! why can't we make reference of Object to null .
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An object can't remove all references to itself, because those references are held by other objects. It's these other references that determine whether an object is eligible for garbage collection or not.
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think the compiler will even allow you to write this = null; but you would have to try it out.
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
compiler won't allow, Campbell Ritchie , thank you All
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"this" and "super" are final variables you can't reassign them values.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unlike this, super is not a variable. And why would it be? There is no object of the super class, only the current object itself (this). There may be methods, constructors and fields from the super class, which is where super is used for - accessing these.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic