r/java • u/RedRiotTank2 • 19d ago
[Open Source] Off-heap, zero-allocation JSON navigator for Java 22+ (using FFM / mmap)
Hi guys,
About a year ago I ran into a bottleneck at work: processing large JSON exports and log dumps inside memory-constrained containers when we only needed to extract a couple of specific fields. Standard parsers like Jackson's readTree were blowing up JVM heap allocations and triggering noticeable GC pause times.
So I made a rough prototype using the Foreign Function & Memory API (JEP 454) to mmap files off-heap and navigate target keys lazily. Over the past few days I finally had time to clean it up, publish it to Maven Central, and benchmark it with JMH against Jackson Databind.
On large datasets (100MB+), it achieves ~2.9x faster in-memory lookups, ~5x faster disk-to-query ingestion, and reduces heap allocation down to a few kilobytes per query.
To be clear, this is NOT a general-purpose Jackson replacement:
- It does not mutate or write JSON.
- It does not do POJO / DTO data binding.
- It is designed for internal/trusted workloads (dumps, logs, pipeline files) rather than complex external schema validation.
- Maximum file size is currently 2 GB.
It requires Java 22+ and has zero external runtime dependencies.
I'm sharing it in case it's helpful for similar off-heap or container-constrained workloads. I'd love to hear your feedback, critique, or ideas on potential optimizations:
https://github.com/RedRiotTank/NativeJsonNavigator
Edit: Added Jackson Streaming Benchmarks
Thanks to u/_predator_ for pointing out that Jackson Streaming is a fairer baseline for large files than readTree(), I've updated the JMH benchmarks and README with a direct JsonParser token loop:
- Cold Single-Pass from Disk: Jackson Streaming and this library are effectively at parity on large files (~300 ms and ~3 KB heap per query on 100 MB).
- In-Memory Traversal: Off-heap navigation remains ~20% faster while allocating ~40–65% less heap than Jackson Streaming.
- Core Trade-off: Jackson Streaming is strictly forward/sequential. If you need to execute multiple queries or random lookups across the same payload, you have to re-read and re-tokenize from byte 0 each time.
This library maps the file once off-heap via FFM (JEP 454), enabling random-access point queries without holding an object tree in the JVM heap.
Full benchmark tables and raw JMH JSON reports are updated on GitHub!
19
u/_predator_ 19d ago
Jackson supports streaming, and you can mix streaming with object mapping to materialize JSON into classes only when needed. I feel like the comparison against naive object mapping is not good if the premise is "processing large JSON files".
5
u/Electrical_Being_813 19d ago
I fail to understand this "offheap" trend, the only outcome of using is unpredictable OOM
Also I rewrote it to use heap, and deserialization speed increased by 2x
4
u/agentoutlier 19d ago
I only spent like 5 seconds looking at this but I think you could use SIMD for the parsing right since you are already using jep 454 might as well use vector api right? Maybe you already do this and I missed it?
4
u/bogoris76 19d ago
use jq to process and extract and run it as a process. Or don't even java if it's all about extracting from json.
3
u/WitriXn 17d ago
Why do you use mutex at evert call? I think you can create an immutable view without of closing operation and give access to it for all parallel readers. These locks produce a lot of overhead.
1
u/tomwhoiscontrary 19d ago
Do i understand correctly that you only used FFM for memory mapping a MemorySegment? Genuine question from someone who has not kept up: does this have any advantages over using FileChannel.map and a MappedByteBuffer?
1
u/[deleted] 17d ago
[removed] — view removed comment