Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize method

 
mike desouza
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arnab karmakar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Arnab
 
Senthil B Kumar
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic