what is the best way to close a ByteArrayOutputStream?
bytearrayoutputstream, heap-memory, java, memory, stream
Solution
You cannot close a ByteArrayOutputStream, since it's close() method is documented as
Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.
This output stream is backed by an array; it is NOT a buffered stream. If you feel it is using too much memory, you should output bytes directly to some endpoint, such as a file or a socket, using an appropriate OutputStream.
Problem
I need to optimize a application that uses too much heap memory. I'm having problem in close a ByteArrayOutputStream variable after using the same. I've tried to do using close() but it does not work. this is the code: ``` ByteArrayOutputStream zipOutTempStream = new ByteArrayOutputStream(); //arquivo.getZipStream() has the XML received by FTP. //STreamEtils is the function who transfers the XML to zipOutTempStream StreamUtils.copiarStream(arquivo.getZipStream(), zipOutTempStream); //Creating a new XML to write over this. File arquivo1 = new File("C:/XML.xml"); if (arquivo1.exists()) { System.out.println("ele existe"); } else { if (arquivo1.createNewFile()) { System.out.println("arquivo criado"); } else { System.out.println("arquivo não criado"); } } FileOutputStream arquivo2 = new FileOutputStream(arquivo1); //Copy the unziped XML to the new xml created. StreamUtils.copiarStream(StreamUtils .uncompressXmlFromZipStream(new ByteArrayInputStream(zipOutTempStream.toByteArray())), arquivo2); arquivo.setZipStream(null); arquivo.setXmlStream(null) return arquivo; ```