Question: ByteBuffer vs MemorySegment for binary (de)serializiation and in-memory buffer pool

Paul Sandoz paul.sandoz at oracle.com
Mon Aug 22 18:21:25 UTC 2022


Hi Gavin,

This is the right place to reach out.

Using MemorySegment will given you far more control over the description (layout) and management (freeing and pooling) than ByteBuffer. Also, if it’s an issue you will also not be constrained by ByteBuffer’s size limitation. Performance wise using MemorySegment should be as good as or better than ByteBuffer.

In many respects MemorySegment is a better API to interact with native memory. ByteBuffer was introduced in Java 1.4 with NIO and had additional design constraints in mind that are less relevant today (such as an internal mutable index).

Paul.

> On Aug 20, 2022, at 10:35 AM, Gavin Ray <ray.gavin97 at gmail.com> wrote:
> 
> Hiya, I hope it's okay that I ask questions here (if not, please direct me elsewhere)
> 
> I tried to ask on StackOverflow without much luck, and there aren't many resources
> available at the moment to read about the Foreign Memory API so I'm reaching out here.
> 
> I'm implementing a basic database on the JVM and am trying to understand the
> technical implications of using either ByteBuffer or MemorySegment for heap page management and in-memory buffer allocation.
> 
> Details are in this thread:
> database - Java: Performance of ByteBuffer versus jdk.incubator.foreign (Panama) Foreign Memory methods (MemoryLayout/Segment) - Stack Overflow
> 
> Would appreciate any information folks would be willing to share =)
> 
> Thank you,
> Gavin Ray
> 
> (By the way, I'm not subscribed to the mailing list, will I be able to reply to responses?)



More information about the panama-dev mailing list