I am using BufferedWriter to write text to files in Java. However, I am providing the custom buffer size in the constructor. The thing is, it is writing to the file in chunks of whatever the size I am giving (for example, if I give the buffer size as 8KB, the files are written once for 8KB). But, when I look at the memory occupied by the bufferedwriter object (using YourKit profiler), it is actually twice the given buffer size (16KB in this case).
I tried to look at the internal implementation to see why this is happening, I see that it is creating a char array with the given size. And when it writes to the array, it makes sense that it occupies twice the buffer size as each char occupies 2 bytes.
My question is, how is BufferedWriter managing to write only 8KB in this case, where it is storing 16KB in the buffer. And is this technically correct? Because each time, it is flushing only 8KB (half) even though it has 16KB in buffer.