memorySegment.asByteBuffer().duplicate() causes multi-MB allocations
Sebastian Stenzel
sebastian.stenzel at gmail.com
Tue Oct 25 06:33:30 UTC 2022
Hi,
using JDK 19 GA and JFR, I noticed an odd behaviour: When calling .duplicate() on a ByteBuffer, you wouldn't usually expect lots of allocations. However, when the buffer is actually a view of a MemorySegment that I retrieved from native code, I observed several hundred of MB allocations for merely a few dozens of ByteBuffers.
Sadly, I don't know what the native code is doing exactly, but when I removed the `.duplicate()`, the allocations are gone. `asReadOnlyBuffer()` works fine, btw.
I suspect there is some copying-to-heap taking place. Oddly enough, the observed figure of MB doesn't match the amount of data contained in those buffers. Either only a few of the buffers actually copy data or only part of the data is copied. Or the numbers are misleading for some reason I don't understand.
Can anyone make sense of this?
Sebastian
More information about the panama-dev
mailing list