4

It'd be helpful if anyone could clarify properly on the 2 points from javadoc of finalize() method in Object class:

1. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked.

What is the significance of 'user-visible' synchronization? Are there any other synchcronization apart from 'user-visible'?

2. The finalize method is never invoked more than once by a Java virtual machine for any given object.

In that case, JVM must maintain unique identity of each and every object ever created vs the information that its finalize method has been invoked. Wouldn't it ultimately grow beyond whatever region it is stored in ?

RRM
  • 2,495
  • 29
  • 46
  • 1
    I would surmise, regarding 1, that this means synchronisation visible to the thread.As for nr 2, once the finalizer of an object has been run that object disappears so no further book keeping is needed. – Erik Apr 29 '14 at 08:48
  • 1s point is fine. For my comment on 2nd point, plz check my comment to Ivan's answer. – RRM Apr 29 '14 at 09:03

3 Answers3

2

What is the significance of 'user-visible' synchronization?

Locks you cana ccess from Java code.

Are there any other synchcronization apart from 'user-visible'?

Yes, the JVM has locks internally for it's use.

JVM must maintain unique identity of each and every object ever created vs the information that its finalize method has been invoked.

Whether the object has been finalized is stored in the header. There is no global id for an object. The only thing unique about it is the reference to the object itself.

Wouldn't it ultimately grow beyond whatever region it is stored in ?

This space is allocated when the object is created.

For more information Object resurrection in Java

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • "Whether the object has been finalized is stored in the header" - this clears up the doubt. Thanks! – RRM Apr 29 '14 at 09:01
  • @RRMadhav Added a link which might be interesting. – Peter Lawrey Apr 29 '14 at 09:03
  • found a related thread on this site, answered by you as well :) http://stackoverflow.com/questions/21809922/how-is-an-object-marked-as-finalized-in-java-so-that-the-finalize-method-wouldn – RRM Apr 29 '14 at 09:13
1

I imagine(!) the second point is realized by deleting that object once finalize() has been called. This way no storage is needed.

DerMike
  • 15,594
  • 13
  • 50
  • 63
  • So, who cleans up the invalid info of GCd objects? Is it "Finalizer" thread or "Reference Handler" thread? – RRM Apr 29 '14 at 08:52
  • @RRMadhav The JVM checks this and set it when an object is added to the finalization queue. It also checks whether finalize() is overridden. – Peter Lawrey Apr 29 '14 at 08:54
1

What is the significance of 'user-visible' synchronization? Are there any other synchcronization apart from 'user-visible'?

I think of "user-visible sync" as any locks or sync code that can be found by virtue of analysing the code as seen by the source compiler. The JVM might actually use a number of other locks and sync primitives internally that is not necessarily a concern for the developer.

JVM must maintain unique identity of each and every object ever created vs the information that its finalize method has been invoked. Wouldn't it ultimately grow beyond whatever region it is stored in ?

No - this is because once JVM calls finalize() - eventually the object will be gc'd.

Bhaskar
  • 7,443
  • 5
  • 39
  • 51