-3

For this line of my Java code:

Scanner s = new Scanner(System.in);

This occurs:

Resource leak: 's' is never closed

What does it mean and why is it occurring?

Abra
  • 19,142
  • 7
  • 29
  • 41
chamanswami
  • 37
  • 1
  • 3
  • 4
    That is a *warning*, not an **error**. And you should probably ignore it. Because closing `s` would also close `System.in`. Which you do not want to do. – Elliott Frisch Jul 16 '23 at 05:23
  • When you're done processing input, then yes, you can close it – OneCricketeer Jul 16 '23 at 05:27
  • 1
    Either a) ignore the message (recommended), b) execute `s.close();` when you're done, or c) use a try-with-resources block `try (Scanner s = new Scanner(System.in)) { ... }` which will close it for you. – Bohemian Jul 16 '23 at 05:32
  • 1
    But you almost certainly don't want to close `System.in`. – user207421 Jul 16 '23 at 05:39
  • Just assure that you no longer require any input from the _[System#in](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/System.html#in)_, and then close the _Scanner_, as usual. Closing the _Scanner_ causes it to also close any resources related to it, in this case, _System#in_. The _System#in_ is a static resource, hence it will be closed for that session of your program. It's import to follow protocols when programming, it's a foundational coding practice. – Reilas Jul 16 '23 at 07:00

2 Answers2

1

Short answer: Ignore it.

Explanation:

This is a warning stating that the Scanner (s) is never closed.

Usually you want to close scanners when you are done with them in order to allow Java to reclaim the memory that the scanner has allocated to temporarily store the data from the InputStream. Avoiding to do so causes a memory(/resource) leak, meaning the memory is claimed permanently.

However in this case, the InputStream is the Systems input (System.in). You do not have to close the Scanner in that case (and propably shouldn't since doing so would also close the Systems input Stream for good). System.in is automatically closed when your Java program terminates, so there is no leak. You should simply ignore it as it doesn't affect anything at all, it's just a warning.

My guess is that you are writing Java in Visual Studio Code. In other IDEs (Netbeans for example) this warning doesn't even occur when using Scanner with System.in. My guess could be wrong tho.

(If I somehow explain something wrong or missed something, someone please respectfully correct me in the comments. Thank you in advance.)

-1

Use try-catch and close your scanner :

    try{
        // your code
        s.close();
    }catch (Exception e){
        System.out.println("Error : " + e.getMessage());
    }
    
Milad Bahmanabadi
  • 946
  • 11
  • 27
  • Additionally, you may want to add the _close_ call within a _finally_ block. – Reilas Jul 16 '23 at 07:06
  • If you're going to use this approach, you might as well use _try-with-resources_ and gain all its benefits. But it's rare one would want to close `System.in`. – Slaw Jul 16 '23 at 07:21