Resource Leak: 'In' Is Never Closed

Java Scanner unassigned closeable value is never closed [Eclipse]

Your IDE is likely warning you about not calling close(), because Scanner implements Closable. As you don't want to close System.in, suppress the warning, and forget about it. You can check your IDE's settings for what it should warn about (resource). It is simply just not detecting that the scanner is using System.in.

IntelliJ IDEA? You have probably enabled one or more of these:

Preferences -> Editor -> Inspections -> Resource management -> ...

Sample Image

how this error Resource Leak scanner is never closed can affect the code

Eclipse is showing you that error because it's generally poor form to leave resources open.

In the specific case of a file handles and sockets related to your Scanner, however, I'm not sure that Scanner overrides the finalize() method in order to explicitly clean up messes that you leave behind when you leave resources open like that. By leaving your Scanner open, you're creating a potential memory leak, and even if GC takes care of it eventually, if you closed the Scanner explicitly, resources would be released a lot sooner than waiting for GC. These types of resources usually include a close(), destroy() or shutdown() method for you for a reason.

Will it affect the "accuracy" or speed of your code? Not in this (simple) case, but if you ever work professionally and write sloppy code for a complex project, you put that project at risk of memory leak, and you probably won't last very long at the job.

How do you fix it? Do what Takendarkk says and add in.close(). If you can avoid so many problems by one simple, small action, why not do it? This article from IBM discusses the issue in more detail.

Resource leak: 'in' is never closed, though it IS closed

Here's how I'd write it:

public void test() throws IOException
{
InputStream in = null;
try {
if(file.exists()) {
in = new FileInputStream( file );
} else {
in = new URL( "some url" ).openStream();
}
// Do something useful with the stream.
} finally {
close(in);
}
}

public static void close(InputStream is) {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

Background of resource leak: stream is never closed

I summarize @Nikolas answer again in other words: There is no problem in Java, but the “problem” (if you want to call it that at all) is the variable. If a variable is declared to a type that implements AutoCloseable (and Stream does), Eclipse apparently reports a warning here that it is not being closed, if it does not find a call to close().

Since this error is Eclipse-related, it is likely that other checking tools will not fail on this and does not need to be “fixed” to pass such checks.

To my understanding, this shows a basic problem of Java, namely that it does not release the object on a variable as soon as it is no longer needed, but at some random point. With resources, that fails. Therefore, resources have to be tracked manually by the developer, where to close them, and manually to be closed. The Java runtime would (hypothetically) have to implement an approach like C++’s std::auto_ptr, then this would not be necessary and if the last reference to the resource was deleted, this could be closed. But it's not like java "thinks".

Scanner is never closed

I am assuming you are using java 7, thus you get a compiler warning, when you don't close the resource you should close your scanner usually in a finally block.

Scanner scanner = null;
try {
scanner = new Scanner(System.in);
//rest of the code
}
finally {
if(scanner!=null)
scanner.close();
}

Or even better: use the new Try with resource statement:

try(Scanner scanner = new Scanner(System.in)){
//rest of your code
}


Related Topics



Leave a reply



Submit