py4j.finalizer
— Py4J Finalizer API¶The py4j.finalizer
module contains global classes that enables the
registration of finalizers, i.e., weak reference callbacks. This module is used
by Py4J to register a finalizer for each JavaObject instance: once there is no
more reference to a JavaObject instance on the Python side, the finalizer sends
a message to the JVM to remove the reference from the Gateway to prevent memory
leak.
The py4j.finalizer
module is necessary because JavaObject instances have
circular references with JavaMethods and hence, they cannot keep their own
finalizer.
A ThreadSafeFinalizer is a global class used to register weak reference finalizers (i.e., a weak reference with a callback).
This class is useful when one wants to register a finalizer of an object with circular references. The finalizer of an object with circular references might never be called if the object’s finalizer is kept by the same object.
For example, if object A refers to B and B refers to A, A should not keep a weak reference to itself.
ThreadSafeFinalizer is thread-safe and uses reentrant lock on each operation.
Registers a finalizer with an id.
id – The id of the object referenced by the weak reference.
weak_ref – The weak reference to register.
Removes all registered finalizers.
clear_all – If True, all finalizers are deleted. Otherwise, only the finalizers from an empty weak reference are deleted (i.e., weak references pointing to inexistent objects).
Removes a finalizer associated with this id.
id – The id of the object for which the finalizer will be deleted.
A Finalizer is a global class used to register weak reference finalizers (i.e., a weak reference with a callback).
This class is useful when one wants to register a finalizer of an object with circular references. The finalizer of an object with circular references might never be called if the object’s finalizer is kept by the same object.
For example, if object A refers to B and B refers to A, A should not keep a weak reference to itself.
Finalizer is not thread-safe and should only be used by single-threaded programs.
Registers a finalizer with an id.
id – The id of the object referenced by the weak reference.
weak_ref – The weak reference to register.
Removes all registered finalizers.
clear_all – If True, all finalizers are deleted. Otherwise, only the finalizers from an empty weak reference are deleted (i.e., weak references pointing to inexistent objects).
Removes a finalizer associated with this id.
id – The id of the object for which the finalizer will be deleted.
Removes all registered finalizers in ThreadSafeFinalizer
and
Finalizer
.
clear_all – If True, all finalizers are deleted. Otherwise, only the finalizers from an empty weak reference are deleted (i.e., weak references pointing to inexistent objects).