Package io.github.jwharm.javagi.interop
Class MemoryCleaner
java.lang.Object
io.github.jwharm.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(io.github.jwharm.javagi.base.Proxy)
and yieldOwnership(io.github.jwharm.javagi.base.Proxy)
.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
free
(MemorySegment address) Run theMemoryCleaner.StructFinalizer
associated with this memory address, by invokingCleaner.Cleanable.clean()
.static void
setBoxedType
(@NotNull Proxy proxy, @NotNull Type boxedType) For a boxed type,g_boxed_free(type, pointer)
will be used as cleanup function.static void
setFreeFunc
(@NotNull Proxy proxy, @NotNull String freeFunc) Register a specialized cleanup function for this proxy instance, instead of the defaultGLib.free(MemorySegment)
.static void
takeOwnership
(@NotNull Proxy proxy) Take ownership of this memory address: when the proxy object is garbage-collected, the memory will automatically be released.static void
yieldOwnership
(@NotNull 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.StructFinalizer
associated 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
-