Closing a ByteArrayOutputStream has no effect?

后端 未结 1 609
面向向阳花
面向向阳花 2020-12-13 12:59

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

相关标签:
1条回答
  • 2020-12-13 13:04

    Does ByteArrayOutputStream.close() really release the memory?

    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.

    0 讨论(0)
提交回复
热议问题