Question

I was given the following phrase in an interview:

The invocation of an Object's finalize() method is the last thing that happens before an object is garbaged collected.

I had to answer by:

  • True
  • False

I've chosen True but it was wrong. Can you please explain me why ?

Was it helpful?

Solution

The order is different:

  1. First the object is collected.
  2. Then the object is finalized.

See http://java.dzone.com/articles/ocajp-7-object-lifecycle-java

Object lifecycle:

  1. Created
  2. In use (strongly reachable)
  3. Invisible
  4. Unreachable
  5. Collected
  6. Finalized
  7. Deallocated

OTHER TIPS

I think it hints that the fact that there are actually other things that can be done / happen to the object before the GC really discards it.

To quote the reference:

[...]The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.[...]

So in this light, the finalizing process isn't the last thing before the GC discards it.

There is no guarantee that finalize() will always be called, or even that garbage collection will run at all.

Suppose that your program ends (either by calling System.exit() or when all running threads reach their end), then the JVM will just quit, it won't clean everything up and call finalize() on all objects.

Therefore, putting cleanup tasks that absolutely must run in a finalize() method is not a good idea.

I guess you can defend both answers, finalize() is called by the garbage collector before it collects the object, but you cannot be sure that will ever be the case before the apllication ends. Not all objects that are allegible to be garbage collected have to be colected. You may never depend on the finalize() method to be called for any object.

The order is wrong, as DR already showed.

An object changes its state to collected when the gc has recognized, that the object is unreachable.

So who should take action to finalize an object before this 'unreachable' condition was detected? In fact it's the garbage collector that marks collected objects for finalization (if the objects finalize method is overridden). And we really don't want to finalize objects that are still reachable, e.g. 'in use'.

Nice question anyway, because you tend to say 'yes it's true'.

You can resurrect the object in the finalize method by making something point to it so the object may not be collected by the GC after calling finalized method. but when the object again becomes available for garbage collection it wont call the finalized method of that object as it has been marked/flagged as finalized. so before GC it may happen the call finalize method or the object can resurrect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top