All of the answers above are great answers, but I don’t think they answer your original question about memory consumption.
In Java, you can view streams in several ways. First you have Raw streams, which are the lowest level stream and interact with the base OS (File, Network, etc.) with minimal memory overhead. Secondly, these are buffered streams that can be used to wrap a raw stream and add some buffering and significantly improve performance. Stream buffering adds a fixed amount of memory overhead for buffering and can be set by your application. Not sure what the default value is, but it is probably something minimal like 32K.
The third type of stream is a memory stream (i.e. ByteArrayInput / Ouput), they use as much memory as you write to them, and will grow as needed, and not get rid of their memory until the reference counter reaches zero (they are no more a long). These threads are very useful, but obviously can consume a lot of memory.
The final type is not really a stream, but an I / O class called Readers, which provide assistance in converting data to and from a stream, as mentioned above. These flows work either on raw materials. buffered or memory stream and will consume the same amount of memory as the main stream that is used.
Jim M.
source share