Tuning buffer sizes
Tuning buffer sizes
Join the DZone community and get the full member experience.Join For Free
Delivering modern software? Atomist automates your software delivery experience.
When the typical read/write size is small, using a buffer can make a significant improvement in performance. However when reading/writing large amounts of data, additional buffered doesn't help and can add a small amount of overhead.
Additionally, it can be tempting to assume; the larger the buffer the better. However is appears a more modest buffer size, around the size of your L1 cache could be better. e.g 8 KB to 32 KB.
In the following test I compared using buffered and unbuffered reads and writes to a temporary file on a tmpfs filesystem (ram disk)
|1||2 MB/s||86 MB/s||3 MB/s||72 MB/s|
|2||4 MB/s||165 MB/s||6 MB/s||147 MB/s|
|4||8 MB/s||333 MB/s||11 MB/s||291 MB/s|
|8||17 MB/s||578 MB/s||24 MB/s||560 MB/s|
|16||34 MB/s||920 MB/s||49 MB/s||983 MB/s|
|32||67 MB/s||1,345 MB/s||99 MB/s||1,679 MB/s|
|64||133 MB/s||1,746 MB/s||198 MB/s||2,518 MB/s|
|128||254 MB/s||2,024 MB/s||391 MB/s||3,387 MB/s|
|256||463 MB/s||2,172 MB/s||742 MB/s||4,104 MB/s|
|512||798 MB/s||2,270 MB/s||1,334 MB/s||4,549 MB/s|
|1,024||1,270 MB/s||2,299 MB/s||2,355 MB/s||4,752 MB/s|
|2,048||1,789 MB/s||2,310 MB/s||3,704 MB/s||4,923 MB/s|
|4,096||2,287 MB/s||2,301 MB/s||5,324 MB/s||4,859 MB/s|
|8,192||2,589 MB/s||2,497 MB/s||6,346 MB/s||6,142 MB/s|
|16,384||2,534 MB/s||2,559 MB/s||5,764 MB/s||5,697 MB/s|
|32,768||2,591 MB/s||2,561 MB/s||5,793 MB/s||5,723 MB/s|
|65,536||2,613 MB/s||2,581 MB/s||5,861 MB/s||5,883 MB/s|
|131,072||2,580 MB/s||2,581 MB/s||5,401 MB/s||5,405 MB/s|
|262,144||1,918 MB/s||1,907 MB/s||3,269 MB/s||3,262 MB/s|
|524,288||1,749 MB/s||1,734 MB/s||2,845 MB/s||2,851 MB/s|
|1,048,576||1,471 MB/s||1,469 MB/s||2,501 MB/s||2,502 MB/s|
The default size of a BufferedInputStream and BufferedOutputStream is 8KB. This is part of the reason why they don't help at this point and larger.
Opinions expressed by DZone contributors are their own.