Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak in Derby database while executing Timer task

I have strange situation. When executing this code, NetBeansIDE profiler shows memory leak (and after some time elapses, application quit because of insufficient memory):

public class SomeClass extends TimerTask {

    private static final Timer timer = new Timer();

    public SomeClass() {
        //Delay 0, repeat every 20ms
        timer.scheduleAtFixedRate(SomeClass.this, 0, 20);
    }

    @Override
    public void run() {

        try (Connection connDB = 
                 DriverManager.getConnection(
                         "jdbc:derby:someDataBase;create=true"); 
             Statement st = connDB.createStatement()) {
             //Some code in normal situation. But the problem
             //exists even without additional code..
        } catch (SQLException ex) {
            Logger.getLogger(SomeClass.class.getName()).log(
                    Level.SEVERE, null, ex);
        }
    }
}

Here are the snapshots:

enter image description here Memory (Heap)

enter image description here Memory (GC) - Surviving generations

Is this some Derby database bug?
Running on JDK8_u31, Derby version/package: db-derby-10.11.1.1-lib.

EDIT 1: When switched to HSQLDB database - no more memory leaks occurred while thousands of connections were opened and closed:

enter image description here Wow! :)

enter image description here

like image 403
Ernestas Gruodis Avatar asked Feb 22 '26 13:02

Ernestas Gruodis


1 Answers

I thought that this might be due to Statement and/or Connection not implementing AutoCloseable. However, according to the JLS, that would be a compilation error ... and the javadocs say that those interfaces do implement it.

It looks like it is a Derby bug, and it might be related to this one:

  • https://issues.apache.org/jira/browse/DERBY-5415

Use the memory profiler to try to see what classes of object are leaking. That should give you some more clues.

like image 103
Stephen C Avatar answered Feb 25 '26 01:02

Stephen C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!