那么,如果 ByteArrayOutputStream 没有正确刷新和关闭,会不会导致内存溢出?我的意思是它们是否有必要放入代码中,否则 Java 会对其进行垃圾回收?
最佳答案
不,一旦对它的最后一个引用丢失,它将被垃圾收集。
根据 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.
此外,如果您查看代码,flush
和 close
都是 ByteArrayOutputStream
类中的空操作(尽管 flush
继承自OutputStream
,在OutputStream
中是一个空操作,除非在具体实现中被重写。
关于Java - 如果没有 flush() 和 close(),ByteArrayOutputStream 是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23164598/