Package org.javagi.interop
Class MemoryCleaner
java.lang.Object
org.javagi.interop.MemoryCleaner
This class keeps a cache of all memory addresses for which a Proxy object
was created (except for GObject instances; those are handled in the
InstanceCache).
When a cached (and owned) Proxy object is garbage-collected, the native
memory is released using g_boxed_free, a custom free-function, or
(as a last resort), g_free.
When ownership of a memory address transfers to native code, the cleaner
will not free the memory. Take and yield ownership with
takeOwnership(org.javagi.base.Proxy) and yieldOwnership(org.javagi.base.Proxy).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidfree(@Nullable MemorySegment address) Run theMemoryCleaner.StructFinalizerassociated with this memory address, by invokingCleaner.Cleanable.clean().static voidsetBoxedType(Proxy proxy, @Nullable Type boxedType) For a boxed type,g_boxed_free(type, pointer)will be used as cleanup function.static voidsetFreeFunc(Proxy proxy, String freeFunc) Register a specialized cleanup function for this proxy instance, instead of the defaultGLib.free(MemorySegment).static voidtakeOwnership(Proxy proxy) Take ownership of this memory address: when the proxy object is garbage-collected, the memory will automatically be released.static voidyieldOwnership(Proxy proxy) Yield ownership of this memory address: when the proxy object is garbage-collected, the memory will not be released.
-
Constructor Details
-
MemoryCleaner
public MemoryCleaner()
-
-
Method Details
-
setFreeFunc
Register a specialized cleanup function for this proxy instance, instead of the defaultGLib.free(MemorySegment).- Parameters:
proxy- the proxy instancefreeFunc- the specialized cleanup function to call
-
setBoxedType
-
takeOwnership
Take ownership of this memory address: when the proxy object is garbage-collected, the memory will automatically be released.- Parameters:
proxy- the proxy instance
-
yieldOwnership
Yield ownership of this memory address: when the proxy object is garbage-collected, the memory will not be released.- Parameters:
proxy- the proxy instance
-
free
Run theMemoryCleaner.StructFinalizerassociated with this memory address, by invokingCleaner.Cleanable.clean().The cleaner action will only ever run once, so any further attempts to free this instance (including by the GC) will be a no-op.
- Parameters:
address- the memory address to free
-