Java - ByteArrayOutputStream 在没有flush() 和close() 的情况下是否安全?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23164598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Is ByteArrayOutputStream safe without flush() and close()?
提问by lannyboy
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?
那么,如果 ByteArrayOutputStream 没有正确刷新和关闭,它会导致内存溢出吗?我的意思是它们是否必须放入代码中,否则 Java 会对其进行垃圾收集?
回答by Shadow Man
No, it will get garbage collected once the last reference to it is lost.
不,一旦对它的最后一个引用丢失,它将被垃圾收集。
Per the javadoc:
根据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.
关闭 ByteArrayOutputStream 没有任何效果。可以在关闭流后调用此类中的方法,而不会生成 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).
此外,如果您查看代码,flush
和close
都是ByteArrayOutputStream
类中的无操作(尽管flush
继承自OutputStream
,但OutputStream
除非在特定实现中被覆盖,否则它是无操作的)。
回答by tjg184
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.
如果您查看 ByteArrayOutputStream 的源代码,您会发现close
该flush
方法是一个 noop,这意味着它们实际上什么都不做。但是,我仍然建议调用它们,因为尽管close
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.
其次,垃圾收集与这两种方法都没有关系。有几种不同的实现(引用计数是一种),但一般来说,当没有更多对您的对象实例的引用时,它将被垃圾收集。
回答by Stephen C
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.
是的。不使用flush()
或不close()
使用ByteArrayOutputStream
. 无论您是否这样做,这对内存使用都没有影响。
The only cases where close()
or flush()
1do 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 doneed to flush or close ... from the "top" of the pipeline ... to ensure that all of the data makes it into the byte array.
close()
或flush()
1与 a 相关的唯一情况ByteArrayOuputStream
是,如果您在包含缓冲组件的输出管道的末尾使用它;例如一个BufferedWriter
。然后您确实需要从管道的“顶部”刷新或关闭...以确保所有数据都进入字节数组。
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 ...)
调用flush()
或没有 GC 影响close()
。无论哪种方式,只要对象保持可达,流的内容就会继续保存在内存中。(相比之下,流对外部资源的读/写需要及时关闭,因为它们有一个需要释放的外部“资源描述符”......)
In summary:
总之:
- It does no harm to
flush()
orclose()
a bareByteArrayOutputStream
. It is just unnecessary. - It is often necessaryto 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.
- 它不会伤害
flush()
或close()
裸露ByteArrayOutputStream
. 这只是不必要的。 - 通常需要关闭以 a 结尾的输出管道
ByteArrayOutputStream
,但这不是因为内存使用或 GC 考虑。 - 只要
ByteArrayOutputStream
对象可达,就会使用内存(至少)。
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.
1 - 事实上,对于一些缓冲流,aflush()
是不够的。例如,当流水线中的一个组件使用块加密算法进行加密时,close()
需要使最后一个部分块被填充、加密和写出。