Java - ByteArrayOutputStream是否安全,没有flush()和close()? [英] Java - Is ByteArrayOutputStream safe without flush() and close()?
问题描述
那么,如果没有正确刷新和关闭,ByteArrayOutputStream会导致内存溢出吗?我的意思是它们是否需要被放入代码中或者Java会垃圾收集它?
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.
按 javadoc :
关闭ByteArrayOutputStream无效。关闭流后,可以调用此
类中的方法,而不会产生
生成IOException。
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
class(虽然 flush
继承自 OutputStream
,但它是一个无操作在 OutputStream
中除非在特定实现中被覆盖。)
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).
这篇关于Java - ByteArrayOutputStream是否安全,没有flush()和close()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!