How big should the buffer of bufferedinputstream be set?

the default is 8m, but the file size is less than 8m. It is not clear how much size should be set for buffering. Should it be less than or equal to the file size? Because downloading files is a batch operation and batch files, you should also take into account the memory consumption of frequently creating buffers.

Apr.25,2022

BufferedInputStream maintains an array in user space to reduce the number of times an application is trapped in the kernel, such as a 15m file. If your buffer size is 3m, it will be trapped into the kernel 5 times, if the buffer size is 8m, then it will be trapped into the kernel twice, if the buffer size is 16m, then you only need to trap once, although reducing the number of times the kernel will be trapped (both will serve as a buffer) But increased memory consumption, and there will be a possibility of waste (for example, the above 16m, actually only read 15m, then wasted 1m), so it is up to you to weigh the buffer size. For example, it doesn't matter if the memory is enough. Of course, the bigger the buffer, the better. For example, if 80% of your files are about 6m on average, you can set 6m, considering batch operations. The frequent application and recovery of the buffer will lose part of the performance, so the buffer pool technology can be used to avoid the reuse of the buffer.

Menu