Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why finalize() method is not called directly?

 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!!
 
Shinil Mohan
Ranch Hand
Posts: 37
Hibernate Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • 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 Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the details.....
 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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 .
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47261
52
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compiler won't allow, Campbell Ritchie , thank you All
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"this" and "super" are final variables you can't reassign them values.
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic