0

So I have the next situation in my program: e.g I have wrapper ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());

I know that if I close most outer stream all inner stream will close automatically, but what will if I close the only inner stream ? Will I have memory leak or something ? Is it problem ? For example I want to close socket (it's automatically closing input/output streams), but should i care about not closed wrappers ObjectOutputStream/ObjectInputStream ???

Sparklll
  • 113
  • 2
  • 7
  • 2
    Well, most of the methods of the outer stream will fail after the inner stream is closed, so the outer stream is not very useful anymore. Some Streams have internal buffers that might be "leaked" if you keep references to these unclosed streams somewhere. – Hulk Jan 28 '21 at 14:34
  • @Hulk yes, that's clear. But is it bad to leave it unclosed? Maybe i will have resourse leaks or garbage collector will do the job ? – Sparklll Jan 28 '21 at 14:37
  • That will depend on the concrete implementation of the stream. There is no *guarantee* that it will not leak anything unless you close it correctly. – Hulk Jan 28 '21 at 14:42
  • "I close most outer stream all inner stream will close automatically (Autocloseable interface)" - this has nothing to do with `Autocloseable`. Actually, there is no guarantee that the inner stream will be closed (even tho it usually will). Just close all your `Closeable` to be sure. – Amongalen Jan 28 '21 at 14:44
  • 1
    Memory leaks may be the least of your worries. Many wrapper streams, including ObjectOutputStream and BufferedOutputStream, use a buffer and don't immediately flush everything you write to them to the underlying stream. So you may end up with missing data if you close the wrapped stream. – Klitos Kyriacou Jan 28 '21 at 14:44
  • Some classes document what exactly they do when closing - however, [`ObjectOutputStream.close()`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/io/ObjectOutputStream.html#close()) does not: "Closes the stream. This method must be called to release any resources associated with the stream." - therefore, nothing is guaranteed. – Hulk Jan 28 '21 at 15:01
  • @KlitosKyriacou But what to do in a situation where, for example, you need to use just ```Object[Output/Input]Stream``` in one part of program and the simple ```Input```/```Output``` stream in another part. And when I interact with sockets closing stream = closing connection. – Sparklll Jan 28 '21 at 15:04
  • Maybe flush the objectOutputStream after every write? – Klitos Kyriacou Jan 28 '21 at 15:08
  • This is focussed on the opposite direction, but it explains why *only* closing the outermost stream is also not correct. The correct approach is using try-with-resources for all streams: https://stackoverflow.com/questions/28276423/is-it-necessary-to-close-each-nested-outputstream-and-writer-separately/28276507, in order to avoid leaks when *creating* an intermediate stream fails. – Hulk Jan 28 '21 at 15:08

0 Answers0