Posts Tagged ‘NativeMemory’
[DevoxxBE2023] The Panama Dojo: Black Belt Programming with Java 21 and the FFM API by Per Minborg
In an engaging session at Devoxx Belgium 2023, Per Minborg, a Java Core Library team member at Oracle and an OpenJDK contributor, guided attendees through the intricacies of the Foreign Function and Memory (FFM) API, a pivotal component of Project Panama. With a blend of theoretical insights and live coding, Per demonstrated how this API, in its third preview in Java 21, enables seamless interaction with native memory and functions using pure Java code. His talk, dubbed the “Panama Dojo,” showcased the API’s potential to enhance performance and safety, culminating in a hands-on demo of a lightweight microservice framework built with memory segments, arenas, and memory layouts.
Unveiling the FFM API’s Capabilities
Per introduced the FFM API as a solution to the limitations of Java Native Interface (JNI) and direct buffers. Unlike JNI, which requires cumbersome C stubs and inefficient data passing, the FFM API allows direct native memory access and function calls. Per illustrated this with a Point
struct example, where a memory segment models a contiguous memory region with 64-bit addressing, supporting both heap and native segments. This eliminates the 2GB limit of direct buffers, offering greater flexibility and efficiency.
The API introduces memory segments with constraints like size, lifetime, and thread confinement, preventing out-of-bounds access and use-after-free errors. Per highlighted the importance of deterministic deallocation, contrasting Java’s automatic memory management with C’s manual approach. The FFM API’s arenas, such as confined and shared arenas, manage segment lifecycles, ensuring resources are freed explicitly, as demonstrated in a try-with-resources block that deterministically deallocates a segment.
Structuring Memory with Layouts and Arenas
Memory layouts, a key FFM API feature, provide a declarative way to define memory structures, reducing manual offset computations. Per showed how a Point
layout with x
and y
doubles uses var handles to access fields safely, leveraging JIT optimizations for atomic operations. This approach minimizes bugs in complex structs, as var handles inherently account for offsets, unlike manual calculations.
Arenas further enhance safety by grouping segments with shared lifetimes. Per demonstrated a confined arena, restricting access to a single thread, and a shared arena, allowing multi-threaded access with thread-local handshakes for safe closure. These constructs bridge the gap between C’s flexibility and Rust’s safety, offering a balanced model for Java developers. In his live demo, Per used an arena to allocate a MarketInfo
segment, showcasing deterministic deallocation and thread safety.
Building a Persistent Queue with Memory Mapping
The heart of Per’s session was a live coding demo constructing a persistent queue using memory mapping and atomic operations. He defined a MarketInfo
record for stock exchange data, including timestamp, symbol, and price fields. Using a record mapper, Per serialized and deserialized records to and from memory segments, demonstrating immutability and thread safety. The mapper, a potential future JDK feature, simplifies data transfer between Java objects and native memory.
Per then implemented a memory-mapped queue, where a file-backed segment stores headers and payloads. Headers use atomic operations to manage mutual exclusion across threads and JVMs, ensuring safe concurrent access. In the demo, a producer appended MarketInfo
records to the queue, while two consumers read them asynchronously, showcasing low-latency, high-performance data sharing. Per’s use of sparse files allowed a 1MB queue to scale virtually, highlighting the API’s efficiency.
Crafting a Microservice Framework
The session culminated in assembling these components into a microservice framework. Per’s queue, inspired by Chronicle Queue, supports persistent, high-performance data exchange across JVMs. The framework leverages memory mapping for durability, atomic operations for concurrency, and record mappers for clean data modeling. Per demonstrated its practical application by persisting a queue to a file and reading it in a separate JVM, underscoring its robustness for distributed systems.
He emphasized the reusability of these patterns across domains like machine learning and graphics processing, where native libraries are prevalent. Tools like jextract, briefly mentioned, further unlock native libraries like TensorFlow, enabling Java developers to integrate them effortlessly. Per’s framework, though minimal, illustrates how the FFM API can transform Java’s interaction with native code, offering a safer, faster alternative to JNI.
Performance and Safety in Harmony
Throughout, Per stressed the FFM API’s dual focus on performance and safety. Native function calls, faster than JNI, and memory segments with strict constraints outperform direct buffers while preventing common errors. The API’s integration with existing JDK features, like var handles, ensures compatibility and optimization. Per’s live coding, despite its complexity, flowed seamlessly, reinforcing the API’s practicality for real-world applications.
Conclusion: Embracing the Panama Dojo
Per’s session was a masterclass in leveraging the FFM API to push Java’s boundaries. By combining memory segments, layouts, arenas, and atomic operations, he crafted a framework that exemplifies the API’s potential. His call to action—experiment with the FFM API in Java 21—invites developers to explore this transformative tool, promising enhanced performance and safety for native interactions. The Panama Dojo left attendees inspired to break new ground in Java development.
Links:
[DevoxxPL2022] Bare Metal Java • Jarosław Pałka
Jarosław Pałka, a staff engineer at Neo4j, captivated the audience at Devoxx Poland 2022 with an in-depth exploration of low-level Java programming through the Foreign Function and Memory API. As a veteran of the JVM ecosystem, Jarosław shared his expertise in leveraging these experimental APIs to interact directly with native memory and C code, offering a glimpse into Java’s potential for high-performance, system-level programming. His presentation, blending technical depth with engaging demos, provided a roadmap for developers seeking to harness Java’s evolving capabilities.
The Need for Low-Level Access in Java
Jarosław began by contextualizing the necessity of low-level APIs in Java, a language traditionally celebrated for its managed runtime and safety guarantees. He outlined the trade-offs between safety and performance, noting that managed runtimes abstract complexities like memory management but limit optimization opportunities. In high-performance systems like Neo4j, Kafka, or Elasticsearch, direct memory access is critical to avoid garbage collection overhead. Jarosław introduced the Foreign Function and Memory API, incubated since Java 14 and stabilized in Java 17, as a safer alternative to the sun.misc.Unsafe
API, enabling developers to work with native memory while preserving Java’s safety principles.
Mastering Native Memory with Memory Segments
Delving into the API’s mechanics, Jarosław explained the concept of memory segments, which serve as pointers to native memory. These segments, managed through resource scopes, allow developers to allocate and deallocate memory explicitly, with safety mechanisms to prevent unauthorized access across threads. He demonstrated how memory segments support operations like setting and retrieving primitive values, using var handles for type-safe access. Jarosław emphasized the API’s flexibility, enabling seamless interaction with both heap and off-heap memory, and its potential to unify access to diverse memory types, including memory-mapped files and persistent memory.
Bridging Java and C with Foreign Functions
A highlight of Jarosław’s talk was the Foreign Function API, which simplifies calling C functions from Java and vice versa. He showcased a practical example of invoking the getpid
C function to retrieve a process ID, illustrating the use of symbol lookups, function descriptors, and method handles to map C types to Java. Jarosław also explored upcalls, allowing C code to invoke Java methods, using a signal handler as a case study. This bidirectional integration eliminates the complexities of Java Native Interface (JNI), streamlining interactions with native libraries like SDL for game development.
Practical Applications: A Java Game Demo
To illustrate the API’s power, Jarosław presented a live demo of a 2D game built using Java and the SDL library. By mapping C structures to Java memory layouts, he created sprites and handled events like keyboard inputs, demonstrating how Java can interface with hardware for real-time rendering. The demo highlighted the challenges of manual structure mapping and memory management, but also showcased the API’s potential to simplify these tasks. Jarosław noted that Java 19’s jextract
tool automates this process by generating Java bindings from C header files, significantly reducing boilerplate.
Safety and Performance Considerations
Jarosław underscored the API’s safety features, such as temporal and spatial bounds checking, which prevent invalid memory access. He also discussed the cleaner mechanism, which integrates with Java’s garbage collector to manage native memory deallocation. While the API introduces overhead comparable to JNI, Jarosław highlighted its potential for optimization in future releases, particularly for serverless applications and caching. He cautioned developers to use these APIs judiciously, given their complexity and the need for careful error handling.
Future Prospects and Java’s Evolution
Looking ahead, Jarosław positioned the Foreign Function and Memory API as a transformative step in Java’s evolution, enabling developers to write high-performance applications traditionally reserved for languages like C or Rust. He encouraged exploration of these APIs for niche use cases like database development or game engines, while acknowledging their experimental nature. Jarosław’s vision of Java as a versatile platform for both high-level and low-level programming resonated, urging developers to embrace these tools to push the boundaries of what Java can achieve.