Well, will ByteArrayOutputStream cause memory overflow if it doesn't properly flush and close? I mean are they necessary to be put in the code or Java will garbage-collect it?
3 Answers
No, it will get garbage collected once the last reference to it is lost.
Per the javadoc:
Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.
Also, if you look at the code, both flush
and close
are no-ops in the ByteArrayOutputStream
class (although flush
is inherited from OutputStream
, it is a no-op in OutputStream
unless overridden in the specific implementation).

- 3,234
- 1
- 24
- 35
-
Thanks. I just want to get a confirmation. Due to my code heavily rely on ByteArrayOutputStream, so I am afraid without flush() or close() properly will cause memory overflow in the further stage. – lannyboy Apr 19 '14 at 01:11
-
Also, as @tjg184 says, it is a good idea to get into the habit of closing streams in finally blocks anyway. – Shadow Man Apr 19 '14 at 01:12
Yes. It is safe to not flush()
or not close()
a ByteArrayOutputStream
. and it makes no difference to memory usage whether you do or don't.
The only cases where close()
or flush()
1 do anything in connection with a ByteArrayOuputStream
is if you have used it at the end of an output pipeline that includes a buffering component; e.g. a BufferedWriter
. Then you do need to flush or close ... from the "top" of the pipeline ... to ensure that all of the data makes it into the byte array.
There are no GC implications for calling flush()
or close()
. Either way, the stream's content will continue to be held in memory for as long as the object remains reachable. (By contrast, streams the read / write to external resources need to be closed in a timely fashion, because they have an external "resource descriptor" that needs to be freed ...)
In summary:
- It does no harm to
flush()
orclose()
a bareByteArrayOutputStream
. It is just unnecessary. - It is often necessary to close an output pipeline that ends in a
ByteArrayOutputStream
, but this is not because of memory usage or GC considerations. - Memory is used (at least) as long as the
ByteArrayOutputStream
object is reachable.
1 - In fact, with some buffering streams, a flush()
is not sufficient. For example, when one of the components in a pipeline performs encryption using a block encryption algorithm, a close()
is need to cause the last partial block to be padded, encrypted and written out.

- 698,415
- 94
- 811
- 1,216
If you look at the source of ByteArrayOutputStream, you can see that close
and the flush
method is a noop, meaning they really do nothing. However, I would still recommend calling these as it's possible the implementation could change although the close
method behavior is explicitly stated in the Javadoc.
Secondly, garbage collection has nothing to do with either of these methods. There are several different implementations (reference counting being one), but in general when there are no more references to your object instance, it will be garbage collected.

- 4,508
- 1
- 27
- 54
-
2It's not possible that the implementation may change, as the behaviour of its close() method is specified in the Javadoc. – user207421 Apr 19 '14 at 01:19