0

I have this code as shown below:

try { FileInputStream is = new FileInputStream("blah.properties"); properties.load(is); }catch(...){ } finally { } 

Finally does not close the InputStream. I have heard varying arguments about Java 1.6 onward not requiring resources such as this InputStream to close, the Garbage collector should be able to take care of it, and there are others who still swear by closing resources.

a) Any conclusive evidence to the above theory?

b) Why doesn't FindBugs detect this, even at "Low Confidence" mode being turned on in the project settings.

7
  • If you are referring try with resources statement, that is from java 1.7 onwards Commented Jan 12, 2018 at 0:24
  • I know about try with resources. My question is about the capability of FindBugs to identify legacy code which might have issues with closing of resources Commented Jan 12, 2018 at 0:28
  • I agree, this question is valid. +1 Commented Jan 12, 2018 at 0:37
  • If you think it's missing something, file a bug report on it. Commented Jan 12, 2018 at 0:42
  • If it wasn’t necessary to close InputStreams in 1.6, why did 1.7 add a try-with-resources statement at all? Commented Jan 12, 2018 at 5:09

1 Answer 1

1

I have heard varying arguments about Java 1.6 onward not requiring resources such as this InputStream to close, the Garbage collector should be able to take care of it

Garbage Collection will close resources that have been left open, but why do that? According to Effective Java 3rd Edition by Joshua Bloch:

Closing resources is often overlooked by clients, with predictably dire performance consequences...Always use try-with-resources in preference to try-finally when working with resources that must be closed.


Why doesn't FindBugs detect this, even at "Low Confidence" mode being turned on in the project settings.

I ran Findbugs in Eclipse using your code. It didn't work for me either, but a FindBugs exception was in Eclipse's Error Log:

!ENTRY edu.umd.cs.findbugs.plugin.eclipse 4 4 2018-01-21 01:17:25.303 !MESSAGE The following errors occurred during FindBugs analysis: !SUBENTRY 1 edu.umd.cs.findbugs.plugin.eclipse 4 0 2018-01-21 01:17:25.303 !MESSAGE Error scanning com/HelloWorld9 for referenced classes !STACK 0 java.lang.IllegalArgumentException at org.objectweb.asm.ClassReader.<init>(ClassReader.java:170) 

Do you see the same thing? Also, note this:

...it looks like FindBugs will never support Java 9. SpotBugs is the replacement.

So I replaced the FindBugs plugin with the SpotBugs plugin and it correctly reported Method may fail to close stream:

spotBugsWorks

If you just replace FindBugs with SpotBugs everything should be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

pardon me, I didn't receive notification for this question of mine to have been answered. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.