Buffer Size for BufferedInputStream
Asked Answered
L

3

5

How to determine the size of Buffer, while using Buffered Input Stream for reading batch of files? Is it based on the File size?I'm using,

byte[] buf = new byte[4096];

If i increase the buffer size,it will read quickly?

Leading answered 24/10, 2013 at 8:47 Comment(4)
It will read more bytes at once.Edmanda
#237361Moustache
Increasing Buffersize would reduce the roundtrip and hence improve performance..Phonoscope
@Phonoscope Reduce what round trip? There are no round trips in file I/O. What magic are you referring to? It improves peformance all right, but not for that reason.Potassium
P
11

The default, which is deliberately undocumented, is 8192 bytes. Unless you have a compelling reason to change it, don't change it.

Potassium answered 24/10, 2013 at 8:52 Comment(6)
Thanks for replying,while using 8192 bytes if suppose my file size is below 8kb,then it won't be a problem?Leading
It's never a problem. What makes you think otherwise?Potassium
Seems like 8K is very small. My computer has 8 Gb of memory. Can you think of any potential disadvantage to setting the buffer size to 500k?Lacuna
Yes, as allocating a buffer of 500k takes much longer than 8k and does not really improve performance in most scenarios.Feu
The size of a page of memory on most computers/operating systems is 4096 bytes, so 8192 is exactly two memory pages. Since OS kernels manage memory on the granularity of pages, and big dynamic memory allocations can be page-aligned, it's often convenient to use a buffer size that's a multiple of the page size. As for performance, it's hard to make any general statements as the entire pipeline from input to output can affect it. It runs at the speed of whichever part is slowest, a bigger buffer can help or hurt performance.Everyplace
@Leading It won't be a problem. The buffer size of 8 K is just the maximum space for one chunk of the file. It doesn't need to use all that space. If you copy a 1-byte file using a 1 million byte buffer, you'll waste 999999 bytes of RAM but the copying will still work just fine. Generally, don't worry about wasting less than 50 KiB unless it's a really tight spot in the program. If the program is slow or uses too much memory, use a profiler that shows you why.Everyplace
S
2

You can easily test it yourself, but it's not really a big issue. A few kilobytes is enough for the buffer, so you'll get good reading speeds.

If you profile your application and do realize that File IO is a performance bottleneck, there are ways to make it quicker, such as memorymapping a file.

Shagbark answered 24/10, 2013 at 8:49 Comment(2)
Thanks for reply,am having a doubt whether it will read block of bytes at once or block by block.If it is reading block by block means,then if we increase the size of buffer,the reading speed will be high,so that am asking.Leading
Thaks for reply.To avoid performance issue,you have mentioned about memorymapping. Please refer some sites or samples for that.Leading
P
0

What you show there is the "byte size" that you are reading into (the array).

If you are reading from a FileInputStream (i.e. non buffered), then changing that size will change the read size, yes.

This is different than the internal buffer used by BufferedInputStream. It doesn't have a getter but you can specify the size in the constructor and "remember it" from that I suppose. Default is 8K which may not be optimal.

Priddy answered 6/8, 2021 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.