Optimal block size in Java threads

I have a theoretical question. Imagine you have an InputStream and an OutputStream . You do not copy content from one to another, and you do not know exactly the size of the content that you need to transfer. What is the best overall block size choice in the write method?

+6
source share
2 answers

Answer: depends. For a general solution, stop worrying about it and just use the library. Common options:

+8
source

The default buffer size for BufferedInputStream and BufferedOutputStream is 8K, and this is usually a good size.

Note. If you read Socket fast enough, you rarely get more than one package, ~ 1.5KB. If you are reading from a disk, you usually get whatever size you request, however the performance does not improve much from 32 KB to 256 KB and will probably depend on the equipment you use.

However, I also found that if you do not conduct a comparative analysis, you rarely see a noticeable difference if you use only 512 bytes as the size of the buffer (Inflator / Deflator streams), i.e. the difference may be 15% or less.

In general, you are unlikely to notice a difference in buffer sizes between 512 bytes and 32 KB. The latter is likely to be more than enough for most situations. I tend to use 256KB as I have a lot of memory and some pre-allocated buffers.

+3
source

Source: https://habr.com/ru/post/885696/


All Articles