Closing a ByteArrayOutputStream has no effect?

Java

Java Problem Overview


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

I want to make sure the memory in ByteArrayOutputStream gets released. Does ByteArrayOutputStream.close() really release the memory?

Thanks.

Java Solutions


Solution 1 - Java

> 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser256239View Question on Stackoverflow
Solution 1 - JavaMichael BorgwardtView Answer on Stackoverflow