-1

I am getting this error "Resource Leak input is never closed " I saw some solutions for this problem online but I am not willing to solve this problem unless it can affect the performance of the program because the code is run fine

import java.util.Scanner;
public class Input {
public static double[][] Get_Ppe_Condition(int row,int col){
    Scanner input=new Scanner(System.in);
the rest of the code....

So my question is do this type of error affect the run of the code with repect to time maybe or can it affect the accurecy of the results? Do you guys suggest to solve it? and what is the better way to solve this type of error? Thanks in advance

  • Why don't you just put a `input.close()` in there? It's warning you about it for a reason. If it made no difference then the warning wouldn't exist. – takendarkk Jan 31 '15 at 00:31

1 Answers1

1

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.

MarsAtomic
  • 10,436
  • 5
  • 35
  • 56
  • Thanks for the link..A Great small tutorial... – Amitesh Rai Feb 19 '15 at 01:34
  • While what you said is applicable to most cases, the System.in resource is an exception. It's not a good practice to close the Scanner explicitly in this case, because reading from it in the future will be prevented, therefore OP should ignore or suppress the warning and leave the closing of this object to the Garbage Collector. – Toni Nagy Oct 14 '21 at 07:55