- Introduction
- Live Demos
- Demo 1: Using a
MemorySegment
- Demo 2: Memory Mapped
MemorySegment
instances - Demo 3: The
Arena
Lifecycle - Demo 4:
InfiniteArena
Providing Unlimited Memory - Demo 5: Structured Access with
MemoryLayout
- Demo 6: Modelling C Structs
- Demo 7: Modelling C Arrays
- Demo 8: Passing Segments to C Functions
- Demo 9: Integrating Java with the Native Library 'zstd'
- Demo 1: Using a
- Interesting Panama FFM Projects and Resources
- Clone Me!
- Safe Harbor Statement
In my talk at jFocus 2023 in Stockholm, I talked about project Panama and more specifically about the Foreign Function and Memory API (FFM).
Below is a list of the live demos made at jFocus 2023. In order to be able to run the demos below, you need to have a JDK 20 build and pass --enable-preview
as a JVM parameter when building and running the code.
This demo shows basic use of a MemorySegment
which is an abstraction of a contiguous 64-bit memory region providing:
-
Spatial, temporal and thread-confinement safety
-
Bidirectional interoperability with arrays and various
ByteBuffer
types.
In this demo, we will see how large MemorySegment
instances (backing up to 64 TiB) can be created by means of memory mapping and sparse files. Read more about memory mapping in this separate article.
This example shows how an Arena
can be used to provide safe and deterministic release of memory resources thereby providing an abstraction of a segment’s lifecycle.
This part outlines an implementation of an Arena
that appears to be able to provide an almost unlimited amount of memory. The concept also allows post-mortem analysis of memory segments used. Read more about the InfiniteArena
in a separate article here.
This sample shows how the concept of MemoryLayout
provides a structured way of describing the layout of linear memory. It also shows how memory segments can be dereferenced using VarHandle
objects that, in turn, can be derived directly from a MemoryLayout
instance.
This class exemplifies how MemoryLayout
constructs can be encapsulated in normal Java classes providing a model for simplified use with retained performance.
This example shows how large arrays of custom MemoryLayout
can be constructed. These arrays do not suffer from the normal Java second-level-of-indirection penalties that normal arrays suffer from. These arrays are furthermore not limited to having 231 - ε elements but are instead only limited by the amount of allocated memory.
In this demo, we explore how to call native C methods using the built-in FFM linker passing a MemoryLayout
to the native function.
In this final example, we will see how easy it is to integrate with an existing native library ("zstd") and call it directly from Java.
Here are some resources that could kickstart your Panama FFM voyage.
Run your own code on JDK 20 today by downloading a JDK 20 Early-Access Build.
-
JEP 434 Foreign Function & Memory API (Second Preview)
-
Open-Source Panama FFM on GitHub: github.com/openjdk/panama-foreign
All code and the entire presentation can be cloned via https://github.com/minborg/articles
The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, timing, and pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation.