memorySegment.asByteBuffer().duplicate() causes multi-MB allocations

Sebastian Stenzel sebastian.stenzel at gmail.com
Tue Oct 25 12:26:38 UTC 2022


I can't reproduce it any longer. 😖

I swear I would not have consulted the mailing list, if the observation had not been that spooky. Especially regarding said discrepency between `asReadOnlyBuffer()` and `duplicate()`.

I still have the .jfr file, though. Is this of any use (other than proving I'm not seeing ghosts)?

> On 25. Oct 2022, at 13:15, Maurizio Cimadamore <maurizio.cimadamore at oracle.com> wrote:
> 
> 
> On 25/10/2022 12:04, Maurizio Cimadamore wrote:
>> ```
>> public MappedByteBuffer duplicate() {
>>         return new DirectByteBuffer(this,
>>                                               this.markValue(),
>>                                               this.position(),
>>                                               this.limit(),
>>                                               this.capacity(),
>>                                               0,
>> 
>>                                               fileDescriptor(),
>>                                               isSync(),
>> 
>>                                               segment);
>>     }
>> ``` 
> 
> Btw, the implementation of `adReadOnlyBuffer` is virtually identical to the above - the only difference is that DirectBuffer::asReadOnlyBuffer creates a new DIrectBufferR (where R stands for Read Only), not just another DirectBuffer. On read-only direct buffers (DirectBufferR), asReadOnly buffer just calls duplicate().
> 
> So, I think it's very strange that one one of them misbehave, since the implementation is so close.
> 
> What do you use to monitor the allocations? One theory is that perhaps one version of the code "escapes" better than the other, so the numbers you see are skewed, and in some cases you see the actual BB allocations on the heap, whereas in the other case you don't see anything because the JVM scalarizes the BB views. But again, the `duplicate` and `adreadOnlyBuffer` are so close to each other that it's hard to imagine meaningful differences in terms of escape analysis (even though escape analsyis can sometimes act in surprising ways).
> 
> Maurizio
> 



More information about the panama-dev mailing list