Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resource leak issues in Eclipse?

I made some tests (using Windows 7, Eclipse Juno 4.2.1 and Java 7 SE) and found that if a method did not explicitly return in the catch-block and if the object was declared outside the try/catch, then no "resource leak" warning was signaled.

Does not generate "resource leak":

    public void extISImReturnNoWarning() {
    InputStream is = null;
    try {
        is = new FileInputStream("A");
        is.available();
    } catch (IOException e) {
    }
}

Small changes in the code generate "resource leaks":

public void locISImReturnHasWarning() {
    try {
        InputStream is = new FileInputStream("A");
        is.available();
    } catch (IOException e) {
    }
}

public void extISExReturnHasWarning() {
    InputStream is = null;
    try {
        is = new FileInputStream("A");
        is.available();
    } catch (IOException e) {
        return;
    }
}

All methods seems functionally identical - so what can the explanation be? If this is a bug, is this an Eclipse or Java issue?

like image 508
mortensi Avatar asked Dec 04 '12 11:12

mortensi


People also ask

How do I find memory leaks in eclipse?

Use the Eclipse Memory Analyzer You may need to refresh your project (F5 on the project). Double-click the file and select the Leak Suspects Report. The overview page allows you to start the analysis of the heap dump. The dominator tree gives quickly an overview of the used objects.

What happens in a resource leak?

Resource leaks are bugs that arise when a program doesn't release the resources it has acquired. Resource leaks can lead to resource exhaustion. In the worst case, they can cause the system to slow down or even crash. Starting with Java 7, most classes holding resources implement the java.

What is resource leak error?

In computer science, a resource leak is a particular type of resource consumption by a computer program where the program does not release resources it has acquired. This condition is normally the result of a bug in a program.


1 Answers

After some research, I've come to the conclusion that this is an Eclipse bug. I've tested it in the latest build I've found on Eclipse Download site (Kepler M3), but the issue persists. I've also found 5-6 bug reports on similar issues reported the last 30 days in the Eclipse project. During the research I also found another peculiar behavior:

    public void makeConnection() throws SQLException {
    Connection connection = null;
    try {
        connection = DriverManager.getConnection("localhost");
        for (int i = 0; i < 1; i++)
            if (i < 1)
                throw new SQLException("Foo");
        connection.commit();
    } finally {
        close(connection);
    }
}

public void close(Connection c) {
}

This code will generate a resource leak warning. However, by removing one of these

  • the call to close(connection)
  • the for-loop

..the resource leak warning will disappear. Strangely enough - no resource leak will be reported in a vanilla case, where you simply create a connection and do not close it.

All in all, I think the resource leak detection in Eclipse Juno might be a little bug-prone for the time being. Maybe it's for the best to ignore resource leaks in Eclipse right now, and wait for Kepler.

Update dec 2014 : Currently running Eclipse Luna, and the issue persists...

like image 130
mortensi Avatar answered Oct 27 '22 23:10

mortensi