title: Memory leak with JDBC / SQLite
body: <p>I have some code which accesses a SQLite database using JDBC.</p>
  
  <p>I've noticed that every time a query is made, the memory usage increases - and it does not go down, even after the connection is closed. </p>
  
  <p>Here's what I'm doing:</p>
  
  <p>1) Closing the <code>PreparedStatement</code></p>
  
  <p>2) Closing the <code>ResultSet</code></p>
  
  <p>3) Closing the connection</p>
  
  <p>Here's a screenshot of the heapdump analysis: </p>
  
  <p><a href="https://i.stack.imgur.com/oCnfj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oCnfj.png" alt="heap dump screenshot"></a></p>
  
  <p>It shows a lot of <code>java.lang.ref.Finalizer</code> and a lot of <code>PreparedStatement</code> and <code>ResultSet</code> objects.</p>
  
  <p>Here's the code (its in scala but should be easily comparable to java):</p>
  
  <pre><code>val conn: Connection = DriverManager.getConnection(url)
  
  
  // Gets strings by a query like SELECT .. WHERE foo = ?
  def getStringsByQuery(query: String, param: String, field: String):Seq[String] = {
  
      val st = conn.prepareStatement(query)
      st.setString(1, param) //value of foo = ?
      st.setFetchSize(Integer.MAX_VALUE)
      st.setMaxRows(Integer.MAX_VALUE)
  
      //Holder of results
      var results = collection.mutable.Seq.empty[String]
  
      val rs: ResultSet = st.executeQuery()
  
      //add results to holder
      while (rs.next())
        results :+= rs.getString(field)
  
      rs.close() //closing ResultSet
      st.close() //closing PreparedStatement
      results
    }
  </code></pre>
  
  <p>Here's the test I wrote to test this:</p>
  
  <pre><code>test("detect memory leak") {
  
      log.info("Starting in 10 sec")
      Thread.sleep(10.seconds.toMillis)
  
      //Calls a method over and over to see if there's a memory leak or not..
      (1 to 1000).par.foreach(i =&gt; {
        val randomWord = getRandomWord() //this produces a random word
        val sql = "SELECT foo FROM myTable where bar = ?"
        val results = getStringsByQuery(sql, randomWord, "bar")
      })
  
      conn.close() //close the connection
      log.info("Closed conn, closing in 30 sec")
  
      Thread.sleep(1.minutes.toMillis)
    }
  </code></pre>
  
  <p>When I run the test - the memory usage steadily increases from 24.6 GB to 33 gb and never goes down (even though ResultSet + PreparedStatement are being closed), and even at the end when the conn is closed and thread sleeps for 1 min - the memory usage still doesn't go down.</p>
  
  <p>Does anyone know what's going on here? I'd appreciate any help.</p>