4

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?

Mark
  • 3,609
  • 1
  • 22
  • 33
user2507974
  • 122
  • 1
  • 2
  • 15

3 Answers3

10

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

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for replying,while using 8192 bytes if suppose my file size is below 8kb,then it won't be a problem? – user2507974 Oct 24 '13 at 09:28
  • It's *never* a problem. What makes you think otherwise? – user207421 Oct 24 '13 at 09:30
  • 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? – John Henckel Nov 06 '17 at 18:46
  • Yes, as allocating a buffer of 500k takes much longer than 8k and does not really improve performance in most scenarios. – Ercksen Mar 22 '19 at 13:44
  • 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. – Lassi May 10 '19 at 15:10
  • 1
    @user2507974 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. – Lassi May 10 '19 at 15:16
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.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • 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. – user2507974 Oct 24 '13 at 08:54
  • Thaks for reply.To avoid performance issue,you have mentioned about memorymapping. Please refer some sites or samples for that. – user2507974 Oct 24 '13 at 09:26
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.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388