File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finalize method" Watch "finalize method" New topic

finalize method

mike desouza

Joined: Oct 14, 2004
Posts: 1
Hi All,
I am new to Java and seek help from your guys. I am trying to call System.gc() and I have a finalize(){S.o.pln("finalize called") } method in my class. Is it always true when ever I run the program, the finalize() is called? When I run in WSAD I got the message "finalize called". But, some reviews tells me it neeed not, Can anybody explain What is the concept to hold to?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
When you invoke System.gc(), you "suggest" to the JVM that garbage collection should run. It is not, however, guaranteed that it will run. Therefore, there is no guarantee that your finalize method will be invoked.

SCJP Tipline, etc.
Arnab karmakar
Ranch Hand

Joined: Oct 04, 2004
Posts: 46
hi mike,

Generally, when u try to run small programs, then first assigning the object to null and then calling System.gc() will have an effect. But when u have got a very big program, then it is not certain that the garbage collector will be successful. The garbage collector is low priority thread.

Senthil B Kumar
Ranch Hand

Joined: Feb 09, 2004
Posts: 160
You are just suggesting the JVM to run the GC, but its upto the JVM to run it or not.

for the finalize method to get executed, you need to nullify the object and try calling GC. Still its not sure , whether it will run or not.

Work like you don't need the money. Love like you've never been hated. Dance like nobody's watching. Sing like nobody's listening. Live like it's Heaven on Earth.
Currently I Reside Here WEBlog
I agree. Here's the link:
subject: finalize method
It's not a secret anymore!