Reputation: 61
Good morning.
Netty is the description part of byteBuffer in official website.
"If java.nio.ByteBuffer is assigned, its contents are filled with 0. This" zeroing "consumes CPU cycles and memory bandwidth. Zeroing is generally bad because the buffers are usually populated from some data sources immediately. "
When assigned in this section, there is no evidence of zeroing that the contents are filled with zeros and consuming CPU cycles and memory bandwidth.
In fact, in Eclipse we have assigned a byteBuffer and have not given any value, so we have confirmed that all values are filled with zeroes. But generally, int a [] = new int [5]; If you declare it like this, this index will be 0 in all indexes, and I think that this part also consumes CPU and memory. Also int a; I think that declaration alone consumes memory.
I do not know why ByteBuffer's zeroing is a fatal problem because I feel like a ByteBuffer or an array after all. If you know, please answer.
Upvotes: 0
Views: 2253
Reputation: 12787
From java specification:
An array initializer creates an array and provides initial values for all its components.
That means that Java VM will always fill with zero's ByteBuffer
(array inside ByteBuffer
), for both DirectByteBuffer
and HeapByteBuffer
as specification requires that. For example, within DirectByteBuffer
you may find next line:
unsafe.setMemory(base, size, (byte) 0);
and for regular array creation:
new byte[size];
JVM will do that (or compiler).
Netty instead uses by default Object Pooling approach. This means that when netty allocates ByteBuf
for you - it is actually just takes existing ByteBuf
from object pool and marks writer
/reader
indexes and capacity
of that buffer, so you always operate only with actual data you put to that buffer based on writer/reader indexes and capacity. There is no "fill with zero operation" in that flow.
However, in case you are using Unpooled heap byte buffers - than zeroing there actually happens, like in the default Java ByteBuffer
.
Zeroing is not fatal operation, however, if you want get maximum performance - elimination of zeroing is very important.
Here is good article explaining how much zeroing may affect performance of array allocation (hint - ~15 times slower on large arrays).
Upvotes: 3