My code does a lot of Input/Output and this often involves the creation of temporary arrays to hold bytes or chars of some size – i often use 4096. Im starting to wonder – without actual tests – to verify if it would be better to pool these arrays. My code would change to something like this
take array from pool
try {
read from one inputStream
write to another outputstream using array
} finally {
return array to pool
}
- it is quicker to take or simply create a byte with 4096 which means some work is required to alloc mem on the heap, clear the 4096 bytes etc.
- a pool seems simpler after all its probably just checking a list taking from the list and returning the array.
UPDATE
I wrote a small program that did two things, created arrays and used an apache commons pool. Both looped a lot of times (100*100*100) and created/took, filled array, then released. I added a few goes in the beginning to warm up the jit and ignored the results of those. Each run ran the create and pool forms a dozen times, alternating between the two.
There was little difference between the pool and create forms. However if i added a clear array to the callback that is fired by apache commons pool when an instance is returned to a pool, the pool became that much slower thanthe created form.
Not really an answer but some important points to consider if one wants to use array pooling as opposed to creation each time an array is required.