0

Found this code at work and my lead dev said, "It could cause a memory leak." and acted like it was no big deal.

 InputStream is = ...
 GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(is));
 someMethod(zis);

No closing the input stream. This is in Android, so its Java 1.6 where InputStream does not implement AutoCloseable. This will cause a memory leak EVERY time it is used, correct? (and I realize this should also all be done in a try/catch/finally.)

eimmer
  • 1,537
  • 1
  • 10
  • 29
  • You can use is.Close() method in android in finally method. This should avoid having a leak. – Prem Dec 14 '13 at 04:45
  • How am I getting voted down for asking a question in a specific scenario so I can better understand how it works? – eimmer Dec 14 '13 at 05:15

1 Answers1

0

It depends on InputStream. E.g. FileInputStream occupies some OS resources which may cause a resource leak if we dont close it. But if InputStream is ByteArrayInputStream there will be no leak.

In any case we should always close InputStream, it should be in finally block or better yet try-with-resources statement.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • In our case we are getting the InputStream generated by HttpResponse.response.getEntity().getContent(). (I'm trying to figure this out on my own, so I really appreciate the help.) – eimmer Dec 14 '13 at 05:32