I had looked at something similar (LRU cache in native code) and ran into
the same issue. AFAICT there is no good work around for this -- you can
either attach your native memory to an auto arena and have it freed at some
arbitrary point in the future, or you can copy the data on to the JVM heap.
Making slice/cloned inputs closeable is a big API change. I worry that
fixing it within Lucene could be very difficult and the API changes would
be very disruptive to consumers of the library.

Also, BufferedIndexInput is very tempting but is not really designed for
the kind of aligned reads you need to do with DirectIO.

On Wed, Sep 2, 2026 at 5:04 PM Sagar Upadhyay <[email protected]>
wrote:

> Hello folks,
>
> I was trying to build a custom off-heap buffer cache on top of Lucene's
> DirectIODirectory. To keep memory bounded and avoid allocating (and freeing
> via Cleaner/GC) an off-heap buffer on every miss, I was looking into
> recycling block buffers from a freelist instead of allocating on every
> miss.
> To do that I need to know that a buffer is no longer referenced by any
> live IndexInput(clones and slices). So was looking at a reference-counting
> based mechanism for that, but it would need close() to be called on
> cloned/sliced IndexInputs, and I am not sure that's possible today.
>
> Has anyone looked into this i.e. a refcounted or recycled buffer cache on
> top of DirectIODirectory? Any help/advice is appreciated.
>
>
> Thanks,
> Sagar
>

Reply via email to