If you look at the source of ByteArrayOutputStream, you will see that the close and flush method is noop, which means that they really do nothing. However, I would recommend calling them, as it is possible that the implementation may change, although the behavior of the close method is explicitly specified in Javadoc.
Secondly, garbage collection has nothing to do with any of these methods. There are several different implementations (reference counting is one), but in general, when there are no more references to the object instance, garbage will be collected.
source share