aspose file tools*
The moose likes Beginning Java and the fly likes why finalize() method is not called directly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why finalize() method is not called directly?" Watch "why finalize() method is not called directly?" New topic
Author

why finalize() method is not called directly?

Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

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

Joined: Jan 10, 2002
Posts: 61224
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

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

Joined: Oct 25, 2008
Posts: 2700

An object is eligible for gc when there are no longer reference to it. Or when it is part of an island of isolation.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

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

Joined: Aug 24, 2011
Posts: 37

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

Joined: Jul 26, 2007
Posts: 10377
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4545
    
    5

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.

luck, db
There are no new questions, but there may be new answers.
Shinil Mohan
Ranch Hand

Joined: Aug 24, 2011
Posts: 37

Thanks for the details.....
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

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

Joined: Apr 06, 2010
Posts: 4392
    
    8

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

Joined: Oct 13, 2005
Posts: 38873
    
  23
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

Joined: Sep 30, 2010
Posts: 290

compiler won't allow, Campbell Ritchie , thank you All
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


"this" and "super" are final variables you can't reassign them values.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why finalize() method is not called directly?