Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a memory leak or a false positive?

This is my code:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class temp {
    public static void main(String[] args) throws FileNotFoundException {
        BufferedReader a = new BufferedReader(new FileReader("a"));
        Scanner scanner = new Scanner(a).useDelimiter(",");
        scanner.close();
    }
}

I get a warning at new Scanner(a) that says (I'm compiling with jdk1.7.0_05.):

Resource leak: '<unassigned Closeable value>' is never closed.

Am I doing something wrong, or is this just a false warning?

like image 598
dln385 Avatar asked Jul 13 '12 02:07

dln385


People also ask

How do you determine if there is a memory leak?

A Memory leak occurs when your computer closes an open program and that program fails to release whatever memory it used while running. One way to check for memory leak is to press and hold down your Windows key and tap the Pause/Break key to bring up System Properties.

What is a memory leak example?

An example of memory leakThe memory leak would occur if the floor number requested is the same floor that the elevator is on; the condition for releasing the memory would be skipped. Each time this case occurs, more memory is leaked. Cases like this would not usually have any immediate effects.

What test is used to detect memory leaks?

Memory testing involves validating a C or C++ application's use of memory and looking for memory leaks or illegal management of memory, such as buffer overwrites. Memory leaks can be catastrophic to an application, resulting in hangs, buffering, or crashes. In the worst-case scenario, they are reported by a customer.


3 Answers

If you split the code like this, will the warning go away?

  Scanner scanner = new Scanner(a);
  scanner.useDelimiter(",");
  scanner.close();
like image 123
Francis Upton IV Avatar answered Oct 16 '22 22:10

Francis Upton IV


Yes, your code has a potential (but not real) memory leak. You assign the return value of useDelimiter(a) to the local variable scanner, but the constructor result is thrown away. That is why you get the warning.

In practice, the return value of useDelimiter(a) is exactly the same object as the one returned from the constructor call, so your code closes the resource just fine. But this is something the compiler/code analysis tool cannot detect as it would have to know the useDelimiters implementation for that.

And a really good code analysis tool should have shown you an additional warning, because you are closing a resource which has not been opened in this method (the return value of useDelimiter). If you had those 2 messages together, the symptoms might have been more clear to you.

like image 43
Bananeweizen Avatar answered Oct 16 '22 22:10

Bananeweizen


Have you try :

Scanner scanner = new Scanner(new BufferedReader(new FileReader("a"))).useDelimiter(",");

If it does not work you have to add a.close();

like image 1
cl-r Avatar answered Oct 16 '22 21:10

cl-r