title: Memory Leak in JdbcRowSet
body: <p>I try to populate a JdbcRowSet with records from a large table (some ten thousand records). I tried two variations (see code below):</p>
  
  <ol>
  <li>Create a connection object, Instantiate using JdbcRowSetImpl(connection), execute the query in a loop.</li>
  <li>Instantiate using JdbcRowSetImpl(DriverManager.getConnection("jdbc:...."), execute the query in a loop.</li>
  </ol>
  
  <p>The first variation results in a memory leak until the heap is full. The second variant has no memory leak. Can somebody explain me why the first causes a memory leak when reusing the connection Object?</p>
  
  <p>thanks</p>
  
  <p>Code for 1.</p>
  
  <pre><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  import javax.sql.rowset.JdbcRowSet;
  import com.sun.rowset.JdbcRowSetImpl;
  
  public class JdbcRowSetMemoryLeak {
  
  /**
   * @param args
   */
  public static void main(String[] args) {
      String username = "user";
      String password = "password";
      Connection connection = null;
      try {
          Class.forName("com.mysql.jdbc.Driver");
          connection = DriverManager.getConnection("jdbc:mysql://localhost/db_ams?user=" + username + "&amp;password=" + password);
      } catch (ClassNotFoundException | SQLException e) {
          e.printStackTrace();
      }
      JdbcRowSet jdbcRS = null;
      for (int i=0;i&lt;150;i++){
          System.out.println(i);
          try {
              jdbcRS = new JdbcRowSetImpl(connection); // &lt;-- Memory is leaking
              jdbcRS.setCommand("Select * from sample_t;");
              jdbcRS.execute();
  //              jdbcRS.close(); &lt;-- Returns a null pointer Exception
              jdbcRS = null;
          } catch (SQLException e) {
              e.printStackTrace();
          }
          try {
              Thread.sleep(1000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  
  }
  
  }
  </code></pre>
  
  <p>Code for 2.</p>
  
  <pre><code>import java.sql.DriverManager;
  import java.sql.SQLException;
  import javax.sql.rowset.JdbcRowSet;
  import com.sun.rowset.JdbcRowSetImpl;
  
  public class JdbcRowSetMemoryGood {
  
  /**
   * @param args
   */
  public static void main(String[] args) {
      String username = "user";
      String password = "password";
      try {
          Class.forName("com.mysql.jdbc.Driver");
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      }
      JdbcRowSet jdbcRS = null;
      for (int i=0;i&lt;150;i++){
          System.out.println(i);
          try {
              jdbcRS = new JdbcRowSetImpl(DriverManager.getConnection("jdbc:mysql://localhost/db_ams?user=" + username + "&amp;password=" + password));
              jdbcRS.setCommand("Select * from sample_t;");
              jdbcRS.execute();
              jdbcRS.close();
              jdbcRS = null;
          } catch (SQLException e) {
              e.printStackTrace();
          }
          try {
              Thread.sleep(1000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  
  }
  
  }
  </code></pre>