Java 关闭 ByteArrayOutputStream 没有效果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2330569/
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
Closing a ByteArrayOutputStream has no effect?
提问by user256239
What does this statement, "Closing a ByteArrayOutputStream
has no effect" (http://java.sun.com/javase/6/docs/api/java/io/ByteArrayOutputStream.html#close()) mean?
“关闭 aByteArrayOutputStream
无效”(http://java.sun.com/javase/6/docs/api/java/io/ByteArrayOutputStream.html#close())这句话是什么意思?
I want to make sure the memory in ByteArrayOutputStream
gets released. Does ByteArrayOutputStream.close()
really release the memory?
我想确保ByteArrayOutputStream
释放内存。难道ByteArrayOutputStream.close()
真的释放内存?
Thanks.
谢谢。
采纳答案by Michael Borgwardt
Does ByteArrayOutputStream.close() really release the memory?
ByteArrayOutputStream.close() 真的释放内存了吗?
No. It does absolutely nothing. You can look at its source code:
不,它完全没有任何作用。你可以看看它的源代码:
public void close() throws IOException {
}
To release the memory, make sure there are no references to it and let the Garbage Collector do its thing. Just like with any other normal object.
要释放内存,请确保没有对它的引用并让垃圾收集器完成它的工作。就像任何其他普通物体一样。
File- and Socket-based streams are special because they use non-memory OS resources (file handles) that you can run out of independantly of memory. That's why closing them explicitly is important. But this does not apply to the purely memory-based ByteArrayOutputStream
.
基于文件和基于套接字的流很特别,因为它们使用非内存操作系统资源(文件句柄),您可以独立地用完这些资源。这就是为什么明确关闭它们很重要。但这不适用于纯粹基于内存的ByteArrayOutputStream
.