JavaOne 2025: 纯 Java 中的函数和内存访问

  • Presented by: Per-Åke Minborg at JavaOne 2025, presenting Function and Memory Access in Pure Java and introducing JEP 454.
  • JEP 454 Background: Evolved from JEP 393 (Foreign-Memory Access API) and JEP 389 (Foreign Linker API), designed to replace Java Native Interface (JNI) with problems like native-first model, high maintenance and deployment cost, and cumbersome data passing.
  • JNI Workflow Example: Defining a native Java method with native modifier, generating C header file with javac -h, and writing the main C application. But it has issues like supporting only primitive types and Java objects, no deterministic memory free, limited addressing space, and inflexible addressing options.
  • Frameworks to Solve Problems: Java Native Access, Java Native Runtime, and JavaCPP but didn't gain traction. Minborg emphasized a more direct, pure Java paradigm.
  • Foreign Memory API:

    • Accessed via [MemorySegment](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/foreign/MemorySegment.html) interface with size, lifetime, and thread confinement controls.
    • [Arena](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/foreign/Arena.html) interface for native memory segment lifecycle control (Global, Auto, Confined, Shared).
    • [ValueLayout](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/foreign/ValueLayout.html) interface models basic data types with carrier type, endianness, and alignment.
    • Examples showed memory allocation and writing with different arena types and value layouts, but manual offset computation was needed. [MemoryLayout](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/foreign/MemoryLayout.html) helps describe memory segment contents.
  • Foreign Function API: Introduced [jextract](https://github.com/openjdk/jextract/blob/master/README.md), a tool to generate Java bindings from native library headers. Example called a native quick sort function.
  • Conclusion: FFM API provides safe and efficient native memory access, general and direct native function access with 100% Java, and the foundation of Project Panama interoperability with tools like jextract.
阅读 8
0 条评论