2

I red from the following link:garbage collection from geeksforgeeks and here it is said that:

The finalize() method is never invoked more than once for any given object.

So, here it is said "more than once" and i'm wondering whether there is possibility that finalize() method is not invoked and garbage collector destroys that object.

Is it possible?

Holger
  • 285,553
  • 42
  • 434
  • 765
Himanshu Roy
  • 43
  • 2
  • 11
  • [This](https://stackoverflow.com/questions/7786946/why-is-finalize-not-being-called) might help. – Andrew S Oct 03 '18 at 15:08
  • 1
    Your newly inserted block is wrong. Even if the garbage collectors runs, there is no guaranty that the `finalize()` method gets executed. The word “definitely” is inappropriate here. All the garbage collector does, is enqueuing objects needing finalization. One or more finalizer threads may process them, but the JVM might terminate before they get to a particular object. Further, a JVM is not required to support finalization at all. – Holger Oct 04 '18 at 08:57
  • 1
    First, questions are not supposed to contain answers. Second, a wrong statement doesn’t become better when you repeat it underneath and say you made it simple. It’s not simple, it’s still wrong. And when you know that there is an already existing Q&A covering the topic better, you can just delete your question. – Holger Oct 04 '18 at 09:54
  • Yes, please delete, unless you can make it much better and different from the linked answer. – maaartinus Oct 04 '18 at 13:35
  • I think nobody has given attention to exact meaning of the question.my question is: assume that garbage collector is definitely going to be run to destroy a particular object in heap(that is object is definitely going to be destroyed by garbage collector) so is there any possibility that garbage collector fully destroyed the object in heap but didn't call the finalize() method on that object? And you all assumed that there is not certainity in the destroying of object by the garbage collector. Now is it clearer? – Himanshu Roy Oct 04 '18 at 14:03
  • The answer in the link i mentioned discusses whether GC is run immediately after an object becomes unreachable but here in my question I want to discuss on the condition that there is full certainity of destroying of object by GC. – Himanshu Roy Oct 04 '18 at 14:07
  • Don’t blame others for not “given attention to exact meaning of the question” when half of your question’s text consist of something you labelled “solution of this question”, containing irrelevant statements. There never was a reason to insert that block. – Holger Oct 05 '18 at 10:26

1 Answers1

2

When the GC has found an object with class where finalize() has been overridden it is added to a queue of objects to have finalize() called on them. It is only after the object has been finalized once, that the GC can clean it up. i.e. this would be on a later GC.

e.g. If an object is in tenured space, it might be found until a full collection is performed, and it will only be cleaned up on a full GC after the finalize method has been called.

For further details, this is the Java 11 Javadoc for Object.finalize()

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#finalize()

so is there any possibility that garbage collector fully destroyed the object in heap but didn't call the finalize() method on that object?

While the object is in the finalization queue, it can't be removed.

And you all assumed that there is not certainity in the destroying of object by the garbage collector.

It won't be destroyed while there is still a strong reference to it.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • "It won't be destroyed while there is still a strong reference to it." This is not needed, since question is assuming that object is definitely going to be destroyed by GC. I only wanted this answer:"It is only after the object has been finalized once, that the GC can clean it up." Thanks! – Himanshu Roy Oct 04 '18 at 17:08
  • @user10086840 or put another way; until the object.finalize() has been called there will be a strong reference to it. – Peter Lawrey Oct 04 '18 at 17:30
  • 1
    This is the wrong terminology. An object is not eligible for finalization when there is a strong reference to it, so you can’t say that there is a strong reference to it “until the object.finalize() has been called”. As discussed in “[finalize() called on strongly reachable object in Java 8](https://stackoverflow.com/q/26642153/)” a JVM may consider an object unreachable when no subsequent computation touches it, even if a method invocation still is in progress. That also applies to the execution of the `finalize()` method. So it *may* “destroy” an object before that, but you won’t notice… – Holger Oct 05 '18 at 10:21
  • @Peter Lawrey.your statement: " until the object.finalize() has been called there will be a strong reference to it. " is not true i think because consider an unreachable object which is detected by a GC and is placed in the finalization queue , now finalize() method has not been called and there is no strong reference to it. So your above statement does not hold true for this case. – Himanshu Roy Oct 05 '18 at 11:27