Is it possible to use the FFM API to "free" a direct NIO buffer?

Julien gouessej at orange.fr
Fri Oct 6 11:00:33 UTC 2023


Hello


Some third party APIs are still using direct NIO buffers despite the numerous
advantages of the FFM API. MemorySegment.asByteBuffer() and
MemorySegment.ofBuffer(Buffer) are helpful to write some new code with the FFM
API while using some APIs heavily relying on direct NIO buffers. I still have to
use some fragile source code to "free" the direct NIO byte buffers in a timely
manner in the meantime as some third party APIs won't migrate to FFM API very
soon, there will be a transition phase. What is the good practice to "free"
direct NIO buffers right now? Is the source code below a good illustration of
what should be done?


final Arena arena = Arena.ofConfined();

MemorySegment.ofBuffer(myDirectByteBuffer).reinterpret(arena, null);

arena.close();


I assume that MemorySegment.ofBuffer(Buffer) uses an automatic arena internally.


Best regards.


More information about the panama-dev mailing list