Java 19 introduces a new external function and memory API (Foreign Function & Memory API), a preview function designed to provide developers with lower-level programming capabilities, allowing Java programs to interact with native code and off-heap memory more efficiently. This API is part of Project Panama, and its goal is to eliminate the barrier between Java and local libraries, allowing developers to call local functions and manipulate local memory safely and efficiently.
In traditional Java development, calling native code through JNI (Java Native Interface) is a common way, but JNI has complexity, security, and performance limitations. Java 19's External Functions and Memory API is committed to providing a more modern and simplified alternative that allows Java to interact directly with local code and memory.
1. Introduction to external functions and memory API
The core goal of the External Functions and Memory API is to provide a simpler and safer way to access external (i.e., non-Java-native) resources. This includes:
- Calling external functions: Allows Java code to call functions in local libraries written in C, C++ and other languages without passing JNI.
- Access local memory: Provides direct access to local memory, allowing Java to operate off-heap memory, thereby improving performance, especially when processing large amounts of data.
The combination of these two functions greatly enhances Java's potential in the fields of high-performance computing and system programming.
2. External Function API
The external function API mainly provides the ability to call local functions. This API allows Java programmers to easily call C functions in the local library without manually writing complex JNI code. This API usesMethodHandle
To dynamically handle external function calls, greatly simplifying interaction with local code.
Basic call process:
- Define function signature: Declare the parameters and return types of the local function to be called through the API.
-
Loading the library:pass
Load the local library in other ways.
-
Calling functions:use
MethodHandle
to execute function calls.
Sample code: Calling Cstrlen
Function
Suppose we want to call the C standard librarystrlen
Function:
import ; import ; import ; import ; public class ForeignFunctionExample { public static void main(String[] args) throws Throwable { // Find and load the symbol "strlen" in the C library SymbolLookup stdlib = (); MethodHandle strlen = ("strlen").get().asHandle((, )); // Use MemorySegment to represent strings MemorySegment cString = (10); // Allocate 10 bytes of local memory ().put("Hello".getBytes()); // Write "Hello" to local memory // Call the strlen function to calculate the length of the string long length = (long) (cString); ("String length: " + length); // Output: 5 } }
explain:
-
SymbolLookup
Classes are used to find functions in C library.strlen
is a function in the C standard library that returns the length of the string. -
MemorySegment
Used to represent local memory areas in Java, similar to pointers to C. -
MethodHandle
Represents a function handle, through which Java can call external functions.
3. Memory API
The Memory API allows Java programs to operate local memory (i.e. off-heap memory) and is no longer limited to JVM-managed heap memory. This is very important for high-performance computing applications, especially in scenarios where large data volumes or the performance impact of GC (garbage collection) is needed to avoid.
The Memory API provides the following core features:
-
Memory allocation:pass
MemorySegment
Allocate memory locally. - Memory operation: Supports read and write operations to local memory, similar to pointer operations in C.
- Memory security: The API has built-in bounds checking of memory access, avoiding common memory overflow and out-of-bounds problems.
Memory API Example: Local memory allocation and operation
import ; import ; public class MemoryApiExample { public static void main(String[] args) { // Allocate local memory (10 bytes) MemorySegment segment = (10); // Get ByteBuffer to operate ByteBuffer byteBuffer = (); (42); // Write integer 42 to local memory // Read integers in local memory (); // Switch to read mode int value = (); ("Value from native memory: " + value); // Output: 42 // Free memory (); } }
explain:
-
Used to allocate off-heap memory, which is not managed by the JVM garbage collector.
- use
ByteBuffer
Operate memory content, similar to that in Java heap memoryByteBuffer
, but it operates on local memory. -
Used to manually free allocated memory to ensure that no memory leaks occur.
4. Advantages of external functions and memory APIs
Compared with traditional JNI, Java 19's external functions and memory API provides many advantages:
Simplify local code calls: Traditional JNI requires writing complex C/C++ code and header files, while the new API allows developers to directly interact with the local library through Java's API, simplifying the calling process.
Improved safety: Memory management problems that are prone to occur in JNI (such as memory leaks or access outages) are effectively solved in the new API through the built-in memory security mechanism. The API provides clear memory lifecycle management capabilities to prevent developers from misoperating operations and causing memory leaks.
Performance improvement: By directly manipulating local memory and calling local functions, Java programs can bypass some performance bottlenecks in the JVM, especially when dealing with large-scale data. Compared with traditional JNI, this calling method is lighter and reduces the performance losses caused by context switching.
Cross-platform support: This API makes Java more easily interacting with local code bases on different platforms, especially when an application needs to call operating system-specific libraries (such as Windows DLLs, Linux SO files).
5. Use scenarios
External functions and memory APIs are especially suitable for the following scenarios:
Interact with local library:
When Java programs need to interact with system libraries (such as graphics processing libraries, encryption libraries, etc.), this API provides a simpler and more performant way than JNI. For example, Java applications can directly call underlying libraries such as OpenGL or OpenSSL.High performance computing:
In scenarios such as scientific computing and big data processing, Java applications can avoid the overhead of the garbage collector by operating off-heap memory, thereby improving performance. For example, when processing large amounts of pictures, videos, or other tasks that require a lot of memory, it can be accelerated through local memory.System programming:
In some scenarios, Java applications require direct operating system resources, such as memory mapped files, network buffers, etc. With the new memory API, Java can more easily implement these low-level operations, approaching the capabilities of system programming languages such as C.
6. Comparison with JNI
characteristic | External functions and memory API | JNI |
---|---|---|
Ease of use | Provides a higher level of API to simplify usage | Need to write local code, with high complexity |
Security | Built-in bounds checking and memory management | Need to manually manage memory, making errors prone |
performance | Efficient local function calls to reduce overhead | Relatively high context switching overhead |
Cross-platform support | Call local libraries directly in Java, cross-platform | Need to be individually compiled for each platform |
Memory management | Built-in lifecycle management to prevent leakage | Need to be managed manually, which can easily cause memory leaks |
7. Future Outlook
Java 19
The external functions and memory APIs are released as preview functions. With the development of the Java language, this function will be further optimized and improved in future versions. Project Panama's goal is to make Java more competitive in cross-language programming and high-performance computing, and external functions and memory APIs are important milestones to achieve this.
Developers can try this API in real-world projects to evaluate its performance improvements and programming ease. In future versions, with the stability and official release of the API, it will become a standard tool for Java to handle local code and memory.
8. Summary
Java 19's external functions and memory API provides developers with stronger local code calls and memory operation capabilities. Through simplified interface design and built-in security mechanisms, developers can more easily call C/C++ functions and manipulate off-heap memory, thereby improving application performance. Compared to traditional JNI, the new API provides a more accessible and secure development experience and is especially suitable for scenarios where high performance and local library integration are required.
This is the end of this article about external functions and memory APIs in the new Java19 feature. For more related Java19 external functions and memory API content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!