关闭ByteArrayOutputStream无效?

时间:2021-03-22 18:11:02

What does this statement, "Closing a ByteArrayOutputStream has no effect" (http://java.sun.com/javase/6/docs/api/java/io/ByteArrayOutputStream.html#close()) mean?

这句话“关闭ByteArrayOutputStream无效”(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.

谢谢。

1 个解决方案

#1


77  

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。

#1


77  

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。