Unpooled.buffer() backing array on Oracle JDK 8 and openjdk-11.0.3

18 views
Skip to first unread message

Priyanka Perera

unread,
May 20, 2020, 2:38:27 PM5/20/20
to ne...@googlegroups.com
Hi,

It appears that with openjdk-11.0.3 the Unpooled.buffer() may return a ByteBuf with a backing array elements initialized to non zero values.

In Oracle JDK 8 however, the elements of the backing array are initialized to zero.

Using netty-4.1.41.Final.

Appreciate if this can be explained.

Is there an API to get a ByteBuf with backing array elements initialized to zero ?.
@Test(invocationCount = 10000)
public void testInitialization() {
final int SIZE = 10000;
ByteBuf byteBuf = Unpooled.buffer(SIZE);

int arrayLength = byteBuf.array().length;
for (int arrayIndex = 0; arrayIndex < arrayLength; arrayIndex++) {
Assert.assertEquals(byteBuf.array()[arrayIndex], 0);
}
}

image.png

Priyanka Perera

unread,
May 21, 2020, 5:17:31 AM5/21/20
to ne...@googlegroups.com
Any insight on this much appreciated.

Frederic

unread,
May 21, 2020, 6:45:01 AM5/21/20
to Netty discussions
Hi,
I checked on my side.
- Using JDK (Oracle) 8 1.8.0_221 (Linux)
- Using OpenJDK 8 1.8.0_232 (Linux)
- Using both Netty 4.1.50
Both versions gives 0 bytes initialized ByteBufs.

However, in general, assuming an allocation gives a zero values array is a wrong assertion (at least in many languages).

If you want to ensure your ByteBuf is set to 0, allocate as you do and then:

  byteBuf.setZero(byteBuf.readerIndex(), SIZE);

Cheers,
Frederic
Reply all
Reply to author
Forward
0 new messages