Promptly freeing the per-thread cached direct buffers when a thread exits

Alan Bateman Alan.Bateman at
Fri Apr 6 08:02:49 UTC 2018

On 05/04/2018 22:45, Tony Printezis wrote:
> Hi all,
> We recently hit another interesting issue with the NIO thread-local
> DirectByteBuffer cache. One of our services seems to create and drop
> threads at regular intervals. I assume this is due to a thread pool
> dynamically resizing itself.
> Let's say a thread starts, lives long enough for its Thread object to be
> promoted to the old gen (along with its cached direct buffer), then exits.
> This will result in its cached direct buffer(s) being unreachable in the
> old gen and will only be reclaimed after the next full GC / concurrent GC
> cycle.
Right, if a short lived thread does I/O with a buffer backed by an array 
in the heap then any direct buffers in its thread local cache won't be 
freed until there is a GC and reference processing. It's something that 
I've prototyped a few times and always leaned towards not exposing 
anything in the API, instead just hooking into the thread exit to clear 
the buffer cache. One thing to watch out for is that the buffer cache 
may not exist (as the thread didn't do any I/O with heap buffers) so 
you'll end up creating (an empty) buffer cache at thread exit. That is 
benign of course but still unsettling to have additional code executing 
at this time.


More information about the core-libs-dev mailing list