RFR: 8357959: (bf) ByteBuffer.allocateDirect initialization can result in large TTSP spikes

Aleksey Shipilev shade at openjdk.org
Wed May 28 12:19:34 UTC 2025


On Wed, 28 May 2025 09:30:56 GMT, Rohitash Kumar <duke at openjdk.org> wrote:

> ByteBuffer.allocateDirect uses UNSAFE.setMemory, causing high time-to-safepoint (100+ ms) for large (100 MB+) allocations.
> 
> This PR applies a simple fix by chunking the zeroing operation within ByteBuffers. A more robust solution would be to add chunking inside UNSAFE.setMemory itself. However Its that straightforward as mentionedby Aleksey in [JDK-8357959](https://bugs.openjdk.org/browse/JDK-8357959)
>>Looks like all current uses we care about are in Buffers. Taking a safepoint within cleaning would open some questions whether any VM code expect to see semi-initialized area we are busy cleaning up. For Buffers, this question does not arise. Therefore, we can do the fix in Buffers first, without changing the Unsafe itself.
>  
> I can pursue that if that is preferred. I chose 1 MB as a chunk size some what arbitrarily I am open to suggestion, if there are better options.
> 
> I added a benchmark for potential perf regression, following are the results.
> 
> **Before**
> 
> Benchmark                                              (bytes)  Mode  Cnt          Score         Error  Units
> ByteBufferAllocationBenchmark.allocateDirectBuffer         128  avgt   25        860.869 ±     170.921  ns/op
> ByteBufferAllocationBenchmark.allocateDirectBuffer        1024  avgt   25       1368.599 ±     320.159  ns/op
> ByteBufferAllocationBenchmark.allocateDirectBuffer     1048576  avgt   25      80169.230 ±     448.075  ns/op
> ByteBufferAllocationBenchmark.allocateDirectBuffer  2147483647  avgt   25  635047372.458 ± 5461328.161  ns/op
> 
> **After**
> 
> Benchmark                                              (bytes)  Mode  Cnt          Score         Error  Units
> ByteBufferAllocationBenchmark.allocateDirectBuffer         128  avgt   25        869.676 ±     160.633  ns/op
> ByteBufferAllocationBenchmark.allocateDirectBuffer        1024  avgt   25       1328.623 ±     282.198  ns/op
> ByteBufferAllocationBenchmark.allocateDirectBuffer     1048576  avgt   25      80224.157 ±     454.906  ns/op
> ByteBufferAllocationBenchmark.allocateDirectBuffer  2147483647  avgt   25  667037308.198 ± 4818648.535  ns/op

Filed [JDK-8357959](https://bugs.openjdk.org/browse/JDK-8357959) -- please rename the PR to "8357959: Long TTSP spikes for large Unsafe.setMemory initializations" to hook it up.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/25487#issuecomment-2915803930


More information about the nio-dev mailing list