RRM
RRM

Reputation: 2689

Clarification on finalize() method in Object class

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 ?

Upvotes: 4

Views: 584

Answers (3)

Peter Lawrey
Peter Lawrey

Reputation: 533930

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

Upvotes: 2

Bhaskar
Bhaskar

Reputation: 7523

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.

Upvotes: 1

DerMike
DerMike

Reputation: 16200

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

Upvotes: 1

Related Questions