users@grizzly.java.net

Re: how to optimize the ByteBuffer memory ?

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Mon, 17 Nov 2008 11:14:06 -0500

Salut,

Survivant 00 wrote:
>
> I think it's more appropriate in this mailing list.
>
>
>
> I have to snippet of codes. I,m looking for a way to optimize the
> ByteBuffer realocation if possible.
>
>
> *Part1 in ProtocolParser*
>
> // Check if buffer is full
> if (processingBuffer.remaining() ==
> processingBuffer.capacity()) {
> // If full - reallocate
>
> // but check if the max length is attain
> if(processingBuffer.capacity() +
> processingBuffer.remaining()<LIMITBB){
> ByteBuffer newBB =
> ByteBufferFactory.allocateView(
> processingBuffer.capacity() * 2,
> processingBuffer.isDirect());
> newBB.put(processingBuffer);
> processingBuffer = newBB;
> WorkerThread workerThread = (WorkerThread)
> Thread.currentThread();
> workerThread.setByteBuffer(processingBuffer);

Why not using a ConcurrentLinkedQueue to store unused ByteBuffer
instance? That way you don't rely on the heap/native memory and instead
you can re-use the instance, or allocate larger byte buffer so you don't
have re-allocate.


A+

-- Jeanfrancois







> } else {
>
> s_logger.info <http://s_logger.info>("BUFFER
> max length was reached");
>
> processingBuffer.clear();
>
> maxBufferReached = true;
>
> return maxBufferReached;
> }
> }
>
> *Part 2 sending data to the clients*
>
> public class Client ....
>
> public void sendToClient(StringBuffer sb) {
>
> ByteBuffer writeBuffer =
> ByteBuffer.allocateDirect(sb.toString().getBytes().length);
>
> writeBuffer.put(sb.toString().getBytes());
>
> writeBuffer.flip();
>
> ......
> }
>
> }
>
>