T
T
themir2014-01-05 15:07:26
Java
themir, 2014-01-05 15:07:26

Does the ByteBuffer.allocate() method provide for caching used buffers, or is a new one created each time?

It is very often used in code ByteBuffer.allocate()to read a stream. Data packets come in 3 sizes: 4, 16, 24 bytes. There are no other options. I don't know how best to proceed, for reasons of saving resources: each time call ByteBuffer.allocate(size), or get 3 ByteBuffer of the right size, and use the appropriate one, clearing it each time.
Actually, my question is: does the method provide for ByteBuffer.allocate()caching of used buffers, or is a new one created each time? It's just that in the future you may have to receive packets of other sizes, and it's expensive to start a ByteBuffer for each of them.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan Lopatin, 2014-01-05
@themir

How did you even come up with the idea that they can be cached? There is also no close() method by which the buffer could be returned to the pool.
There is no caching there, and it is not necessary. As a rule, such a buffer is created one per processing thread, and not per request, as you have. So no separate pool is required.

A
afiskon, 2014-01-06
@afiskon

The truth is somewhere in the sors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question