5

I got a warning in Eclipse with the following code:

Code:

Scanner money = new Scanner(System.in);
System.out.println(money.nextLine());
//money.close();

Warning:

Description Resource    Path    Location    Type
Resource leak: 'money' is never closed  apples.java /SwordsNMoney/src   line 6  Java P

What is this warning and what does 'Resource Leak' mean?

Thank you.

0xCursor
  • 2,242
  • 4
  • 15
  • 33
Divyanshu Jimmy
  • 2,542
  • 5
  • 32
  • 48
  • 1
    Welcome to Stack Overflow. Please read [Stack Overflow: How to ask](http://stackoverflow.com/questions/how-to-ask) and [John Skeet's Question Checklist](http://msmvps.com/blogs/jon_skeet/archive/2012/11/24/stack-overflow-question-checklist.aspx) to find out how to ask a good question that will generate good useful, answers. – Our Man in Bananas Jul 04 '14 at 11:33

5 Answers5

13

Resource leak is generally an erroneous pattern of resource consumption where a program does not release the resource it has acquired. This can lead to poor services.

Garbage collection can only manage memory, not other system resources. If your Java program has plenty of free memory, garbage collection will not be triggered automatically.

All OSes have limits on the number of sockets, file handles, etc., that can be open. Thus, the unintentional maintenence of references to non-memory resources can lead to a resource leak. So it is extremely important to manage non-memory resources.

Classes which utilize non-memory resources should provide ways to explicitly allocate/deallocate those resources. We need to explictly call close() methods for deallocation of file descriptors in finally{}, as it will execute whether or not an exception is thrown.

0xCursor
  • 2,242
  • 4
  • 15
  • 33
SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • java.util.Scanner money ; here money is a variable or an object and what do you mean by plenty of free memory in lieu with garbage collection ? – Divyanshu Jimmy Jul 04 '14 at 13:55
  • first of all money is a reference variable here until we we create a object using `new` once object is created it will hold reference of the object and secondly regarding free memory first you should know that the JVM memory or heap is divided into 3 parts Young generation,old generation and permanent generation. As long as the young gen area is free garbage collection does not takes place [Refer this](http://www.journaldev.com/2856/java-jvm-memory-model-and-garbage-collection-monitoring-tuning). – SparkOn Jul 04 '14 at 14:51
  • **plenty of free memory** means the young generation area is not having much rush all the object are doing their job and getting garbage collected – SparkOn Jul 04 '14 at 14:51
3

Scanner opens an underlying OS's file descriptor (or file channel, or stream), which typically is written in a non-managed(typically C language).

A stream kept open, can sometimes stay opened until the kernel decides to close it(like, after the program has completed execution... highly implementation dependent).

Hence its a good idea to close the resource explicitly.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
3

You need to call 'close' on IO classes.

You can use try catch and finally block and in the finally block you can close the scanner.

From the docs

Close

If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked. If this scanner is already closed then invoking this method will have no effect.

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

It could be, that the underlying resource, which is a file descriptor (in this case System.in) is never released, and thus blocked if you do not close it properly.

As far as I know, Java has some kind of handling-mechanisms to close open Scanners automatically. Someone knows more about this?

paubo147
  • 748
  • 4
  • 8
1

Leak in general defined as not gets garbage collected. Examples are thread leak, object leak- In such cases memory occupied by thread, object not get garbage collected.

Here you are not closing money reference variable. So reference not gets released. So its referred as Reference leak.

rs4706
  • 61
  • 4