• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When finalize() will be called?

 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above example, how the finalize method is called automatically?
 
Satish Kota
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After the main method execution completed 2 objects are eligible for Garbage Collection process. Whena finalise method is called cant be guranteed
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, always mention the source of the mock questions you post.

finalize() will be called when the garbage collector attempts to garbage collect the object for the first time. Because objects are not always garbage collected, you cannot say definitely that finalize() will be called.
 
Greg L Tonn
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the code shown above is actually an infinite loop. This loop creates a new Box object every time. If there was a mechanism in the loop to control it and limit it to run only once then what Satish said is true there would be two objects ready for GC at the end of the programs lifetime.

K&B (chapter 3) says that;

For any given object, finalize() will be called only once (at most) by the garbage collector.


Hope it helps.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing is, Greg, is that the garbage collector does not have to run at the end of a program. So the program could just return from main and no finalizers need be called.
 
Greg L Tonn
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Berry,

So if the program ends, returns from main, the objects still sit out there on the heap, right?

If that is the case when the GC does run does it would it even attempt to call the finalizer (even through I know it's not a guarantee)?

Thanks
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case of the program ending - and the JVM exiting - the heap is wiped out, the GC does not necessarily go and collect each object and run its finalizer. If an object is garbage collected then it is part of the contract that the finalizer must be run for that object. If the GC attempts to collect the object a second time the finalizer will not be run.
 
Henry Wong
author
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if the program ends, returns from main, the objects still sit out there on the heap, right?


When a program ends, causing the JVM to exit, all resources are cleaned up by the operating system. The OS doesn't know the difference between the heap, the string cache, the stack, etc., it is all just user memory.

The memory related to the JVM process are returned to free memory.

If that is the case when the GC does run does it would it even attempt to call the finalizer (even through I know it's not a guarantee)?


The GC is part of the JVM process. There is no "when the GC does run".

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic