Is it good practice to clear () bytebuffer after allocation?

I inherited some Java code that writes messages to a socket using local ByteBuffer:

public void sendMessage(){
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
    byteBuffer.clear();
    byteBuffer.putInt((int) 128);
}

It seems that the call is clear()not needed. Would it be guaranteed to return a freed memory block?

+4
source share
2 answers

No need to call clear()after allocateDirect.

What allocateDirect:

The new buffer position will be zero, its limit will be its capacity, its label will be undefined, and each of its elements will be initialized to zero.

What clear:

​​ , , .
[..]
, , , , .

, ByteBuffer clear() ( , clear() reset ).

+8

, ( ).

, , .

, , .

, , , , :

ByteBuffer FillBytes(ByteBuffer buf) 
 { 
    buf.clear();
    //--- 
 }

, , , , - , dev reset , , , (, - , , reset).

+2

All Articles