08

Demo 08 — Project Panama: C++20 via FFM

Quarkus 3.33.1 / JDK 25 LTS ⏱ ~8 min JDK 25
cd quarkus-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/ZGC
  • jvmstats_cpu_profile() — detect GC-dominated CPU spike patterns
  • jvmstats_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

Three-stage Dockerfile

Stage Image Purpose
cpp-builder ubi9 dnf install gcc-c++ cmake make → compile .so
java-builder maven:3.9-eclipse-temurin-25 Build Quarkus JAR
runtime ubi9/openjdk-25-runtime 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

Reference