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 new Proxy object is created, the reference count in the cache is
increased. When a Proxy object is garbage-collected, the reference count in
the cache is decreased. When the reference count is 0, the memory is
released using GLib.free(MemorySegment)
or a specialized method.
When ownership of a memory address is passed to native code, the cleaner
will not free the memory. Ownership is enabled/disabled with
takeOwnership(Proxy)
and
yieldOwnership(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 all proxy objects are garbage-collected, the memory will automatically be released.static void
yieldOwnership
(@NotNull Proxy proxy) Yield ownership of this memory address: when all proxy objects are 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 all proxy objects are garbage-collected, the memory will automatically be released.- Parameters:
proxy
- the proxy instance
-
yieldOwnership
Yield ownership of this memory address: when all proxy objects are 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()
.- Parameters:
address
- the memory address to free
-