Using the off-heap memory to store and process large amounts of data didn't change in Java since Java SE 4, when ByteBuffer was introduced. Since then, operating systems moved from 32 bits to 64 bits, and the available RAM in a regular machine moved from megabytes to gigabytes, and more. Another API was much needed, as a ByteBuffer is a 32 bits buffer, not enough for modern applications. First published as a preview feature in Java SE 19, the Foreign Function and Memory API made it as a final feature in Java SE 22. The Memory part brings several new concepts. Among them Arenas and MemorySegments now give you the possibility to manage gigabytes of contiguous off-heap memory, with a very elegant layout model. On the other hand, MemoryLayout allows for a C-struct like organization of your data in memory.
This presentation shows you this complex API, in a step by step approach. It explains how your data is organized and aligned in memory, and the impact it has on the API. It also focuses on the delicate use of VarHandle, a critical element to access your data. It then shows you how you can load large files in memory segments, and shows you the performance you can get in the processing of billions of data elements.