How to Run Local LLMs via Java 22 Panama FFM
I've been experimenting with Project Panama (Foreign Function & Memory API) in the latest JDK to interface directly with llama.cpp. Instead of just hitting an API, I built libargus.cc to create a clean ABI that exposes a structured API specifically for the JVM. This setup allows us to talk directly to llama.cpp, whisper.cpp, and even ggml compute graphs without the usual performance tax.
The real magic here is the memory management. I managed to achieve zero-allocation on the hot paths. By using confined Arenas, memory segments for prompts and tokens are allocated exactly once. We are passing raw pointers straight down to the C level, which completely eliminates the headache of primitive array cloning and massive heap churn that usually plagues Java-based AI implementations.
I had to manually map out the native structures from llama.cpp and whisper.cpp to ensure the compiler's padding matches perfectly—one wrong byte and your memory access is toast. For anyone wanting to see how this integration looks or how the memory segments are handled, you can check out the implementation logic:
// Concept of how the ABI is structured for the JVM to interface safely
// ensuring padding matches the native llama.cpp structures
struct ArgusNativeContext {
void* llama_context;
void* model_state;
// Precise padding alignment for Panama FFM
char padding[8];
// ...
};I'm currently using this engine as the foundation for a spatio-temporal memory layer (L-TABB) that I'm building to eventually replace traditional RAG workflows. If you're hacking on low-latency systems or playing around with modern JDK features, this is a massive leap forward for developer experience in the AI space.
https://libargus.cc
https://projectargus.cc