08
Demo 08 — Project Panama: C++20 via FFM
Spring Boot 4.0.5 / JDK 25
⏱ ~8 min
JDK 25
Run this demo
View source on GitHub ↗
cd demo-08-panama
chmod +x demo.sh
./demo.sh
The Foreign Function & Memory API (JEP 454, finalized JDK 22) calls a native C++20 shared library from pure Java. No JNI wrapper code, no platform-specific compilation pipeline.
What the native library does
Three C++20 functions using std::span, std::ranges, and structured bindings:
jvmstats_recommend_gc()— analyse GC pause times, recommend G1/Shenandoah/ZGCjvmstats_cpu_profile()— detect GC-dominated CPU spike patternsjvmstats_recommend_memory_mb()— compute right-sized memory request from RSS samples
The Panama pattern
try (Arena arena = Arena.ofConfined()) {
// allocateFrom() — JDK 22+ final API (not allocateArray())
MemorySegment data = arena.allocateFrom(JAVA_DOUBLE, myArray);
MemorySegment outP99 = arena.allocate(JAVA_DOUBLE);
int gcCode = (int) recommendGc.invoke(data, myArray.length, outP99);
double p99 = outP99.get(JAVA_DOUBLE, 0);
} // all native memory freed here — zero leaks possible
Spring Boot integration
The native library is loaded via a Spring @Configuration class:
@Configuration
public class PanamaConfig {
@Bean
public Linker nativeLinker() {
return Linker.nativeLinker();
}
}
JVM flag required to enable native access:
--enable-native-access=ALL-UNNAMED
Three-stage Containerfile
| Stage | Image | Purpose |
|---|---|---|
| cpp-builder | ubi9 |
dnf install gcc-c++ cmake make -> compile .so |
| java-builder | maven:3.9-eclipse-temurin-25 |
Build Spring Boot JAR |
| runtime | eclipse-temurin:25-jre |
Copy .so + JAR, ldconfig |
No subscription required — gcc-c++ is in UBI9’s freely accessible appstream repo.
Try the endpoints
curl -s http://localhost:8080/demo | python3 -m json.tool
# Analyse custom GC pause data
curl -s -X POST http://localhost:8080/gc-recommend \
-H "Content-Type: application/json" \
-d '[10,12,180,11,175,14]' | python3 -m json.tool