Optimal block size in java streams
I have a theoretical question. Let's imagine you have an InputStream
and an OutputStream
. You nead to copy content from one to the other and you don't know 开发者_StackOverflow中文版exactly the size of the content you need to transfer. What is the best choice in general of the block size in the write
method?
The answer is: it depends. For a general solution, stop worrying about it and just use a library. Common choices:
- Apache Commons IO
IOUtils#copy()
orcopyLarge()
, or - Google Guava's
ByteStreams#copy()
The default buffer size for BufferedInputStream and BufferedOutputStream is 8 KB and this is typically a good size.
Note: if you are reading a Socket fast enough, you will rarely get more one packet, ~1.5 KB. If you are reading from disk, you typically get whatever size you ask for, however the performance doesn't improve much from 32 KB to 256 KB and is likely to be dependant on the hardware you use.
However I have also found, unless you are benchmarking you rarely see a noticeable difference if you use only 512 bytes as a buffer size (which Inflator/Deflator streams do) i.e. the difference might be 15% or less.
In summary, you are unlikely to notice a difference with buffer sizes between 512 bytes and 32 KB. The latter is likely to be more than enough for most situations. I tend to use 256 KB as I have a lot of memory and few pre-allocated buffers.
精彩评论