Part IV — Foreign Function & Memory (FFM)

Harness the power of native code integration and advanced memory management with Java's Foreign Function & Memory API—a safe, efficient alternative to JNI.

What You'll Learn

Safely call native libraries without the complexity of JNI. Master fine-grained off-heap memory management with MemorySegment and arenas, enabling high-performance scenarios from cryptography to image processing to ML inference.

Chapters in This Part

Chapter 11. FFI Basics

Introduction to the Foreign Function Interface (FFI) and why the FFM API represents a safer, more maintainable path to native interoperability than legacy JNI approaches. Understand the architectural design that enables efficient native calls with full Java safety guarantees.

Chapter 12. MemorySegment Allocation

Learn precise off-heap memory management through MemorySegment and the arena allocation model. Discover how modern memory APIs support both arena-scoped and implicit temporal scopes, enabling zero-copy operations and predictable cleanup.

Chapter 13. Linker and Symbols

Master the linker API and symbol lookup mechanisms that bridge Java code and native function signatures. See how to dynamically load native libraries, invoke complex C functions, and handle platform-specific differences elegantly.