6

When I construct a new BufferedReader it is providing me a buffer of 8192 characters. What is the logic/reason behind this?

8192 = 2 to the power of 13
user207421
  • 305,947
  • 44
  • 307
  • 483
Sanjit Kumar Mishra
  • 1,153
  • 13
  • 32
  • 1
    The source of BufferedReader says "The default is large enough for most purposes." http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/io/BufferedReader.java – Arjan May 24 '16 at 03:58

5 Answers5

9

Traditionally, memory managers and paging files in the operating system work on pages that are sized in powers of 2. This allows very efficient multiply/divide operations to be performed with left/right shift operations. When working with a buffer, the worst case scenario is to have a buffer with size 1 byte longer than the page size (that would result in an extra page swap with very low benefit). So the default buffer sizes will also tend to be implemented in factors of two.

I'd assume (but have not checked) that the JVM looks for buffers like this and attempts to align them on page boundaries.

Why does this matter? Page misses are quite expensive. If you are doing a ton of IO, it's better to avoid the case where the page backing the buffer gets swapped out to disk (kind of defeats the purpose of the buffer). That said, for most applications, this is a micro-optimization, and for the vast majority of cases, the default is fine.

For reference, Windows and Linux both currently use a 4KB memory page size. So the default buffer on BufferedReader will consume exactly 2 pages.

Kevin Day
  • 16,067
  • 8
  • 44
  • 68
5

As the BufferedReader Javadoc says

The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

The default was chosen as being "large enough" (which I would interpret as "good enough").

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

8192, as you said, is 2^13. The exact reason for this number being the default is hard to come by, but I'd venture to say it's based on the combination of normal use scenarios and data efficiency. You can specify a buffer size of whatever you want, though, using a different object constructor.

BufferedReader(Reader in, int sz)

Creates a buffering character-input stream that uses an input buffer of the specified size.

https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html and BufferedReader default buffer size? will provide further insight.

Community
  • 1
  • 1
willbattel
  • 1,040
  • 1
  • 10
  • 37
1

There is a JDK ticket https://bugs.openjdk.org/browse/JDK-4953311 that states

Most OSes that we support uses a buffer size of 8192 (8K) bytes for their IO buffering, and this is also the buffer size used by Microsoft VM on Win32. We should change the default buffer size in these two classes to 8K.

Yevhenii Melnyk
  • 553
  • 4
  • 16
0

8192 is 2^13 and also reveals much information regarding the RIGHT v WRONG encoded in all we do. If one takes away or adds to the author intent, he modifies and therefore corrupts the entire thing. Try to add or take away from something perfect... good luck!