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?
解决方案
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).