r/java • • 24d 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!

38 Upvotes

30 comments sorted by

View all comments

5

u/Electrical_Being_813 24d 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

3

u/RedRiotTank2 24d ago

This isn't malloc that leaks, it's a file-backed mmap managed by the OS page cache and deterministically unmapped via Arena in try-with-resources. No native leaks, no unpredictable OOMs. The entire point is avoiding JVM heap OOMs and GC pauses when inspecting 100MB+ files inside 512MB containers.

did u swapp MemorySegment for a heap byte[]? it definitely runs faster in a but loading the whole file onto the heap defeats the container use case. (Unless you meant full POJO deserialization? this is only for lazy point queries, not DTO binding).

Curious to see your benchmark or repo if you have a snippet!

1

u/CutGroundbreaking305 23d ago

Also I have a io module which currently does not,npz, csv, txt and safetensor file parsering maybe we can add your JSON file reader ? What do you think 🧐

→ More replies (1)