我需要关闭 ByteArrayInputStream 吗?
简短的问题,
我在一些旧代码中看到 ByteArrayInputStream
创建如下:
new BufferedReader(new InputStreamReader(new ByteArrayInputStream(somebytes)));
然后使用 BufferedReader
逐行读出 somebytes
.
一切正常,但我注意到 BufferedReader
从未关闭。
这一切都在长时间运行的 websphere 应用程序中进行,somebytes
并不是很大(最多 200k),它每周只被调用几次,而且我们没有遇到任何明显的内存泄漏。所以我希望所有对象都被成功垃圾收集。
我总是(一次)了解到输入/输出流需要在 finally
语句中关闭。 ByteStreams
是这条规则的例外吗?
亲切的问候 杰罗恩.
Short question,
I saw in some old code where a ByteArrayInputStream
was created like:
new BufferedReader(new InputStreamReader(new ByteArrayInputStream(somebytes)));
And then the BufferedReader
is used to read out somebytes
line by line.
All working fine, but I noticed that the BufferedReader
is never closed.
This is all working in a long running websphere application, the somebytes
are not terrible big (200k most), it is only invoked a few times a week and we're not experiencing any apparent memory leaks. So I expect that all the objects are successfully garbage collected.
I always (once) learned that input/output streams need to be closed, in a finally
statement. Are ByteStreams
the exception to this rule?
kind regards
Jeroen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不必关闭 ByteArrayInputStream ,当它没有被任何变量引用时,垃圾收集器将释放流和一些字节(当然假设它们没有被引用)其他地方)。
但是关闭每个流始终是一个好习惯,事实上,也许创建流的实现将来会发生变化,您将读取文件而不是原始字节?另外,静态代码分析工具(如 PMD 或
FindBugs)(请参阅评论)很可能会抱怨。如果您厌倦了关闭流并被迫处理不可能的 IOException,您可以使用 IOUtils:
You don't have to close
ByteArrayInputStream
, the moment it is not referenced by any variable, garbage collector will release the stream andsomebytes
(of course assuming they aren't referenced somewhere else).However it is always a good practice to close every stream, in fact, maybe the implementation creating the stream will change in the future and instead of raw bytes you'll be reading file? Also static code analyzing tools like PMD or
FindBugs(see comments) will most likely complain.If you are bored with closing the stream and being forced to handle impossible
IOException
, you might use IOUtils:关闭读者始终是一个好习惯。然而,不关闭 ByteArrayInputStream 不会产生那么严重的潜在负面影响,因为您没有访问文件,只是访问内存中的字节数组。
It is always good practice to close your readers. However not closing a ByteArrayInputStream does not have as heavy of a potential negative effect because you are not accessing a file, just a byte array in memory.
正如 @TomaszNurkiewicz 提到的,关闭打开的流总是好的。让它自己执行 try 块的另一个好方法。将 try 与资源一起使用,例如......
这里 Workbook 和 InputStream 都实现 Closeable Interface,因此一旦 try 块完成(正常或突然),流肯定会关闭。
As @TomaszNurkiewicz mentioned it's always good to close the opened stream. Another good way to let it do the try block itself. Use try with resource like.......
here Workbook and InputStream both implements Closeable Interface so once try block completes ( normally or abruptly), stream will be closed for sure.
资源需要在
finally
(或同等方式)中关闭。但是如果你只有一些字节,那没关系。尽管在编写时,请注意在快乐的情况下flush
。Resources need to be closed in a
finally
(or equivalent). But where you just have some bytes, no it doesn't matter. Although when writing, be careful toflush
in the happy case.