Increased memory usage of my program in JAVA SE


I have created a simple program in JAVA SE with NETBEANS where I have 3 buttons and every time I press a button I send information to my database, at all times I have the connection open and every time I press the button

//Creo la conexión

static Connection connection = null;
connection =DriverManager.getConnection( "jdbc:mysql://", "usuario", "contraseña");

//Dejo la conexión abierta y cada vez que pulso un botón realizado este método.
private void insertarRegistro(int id_persona, int tipo_acceso){
            PreparedStatement pst = null;
        try {                
                String sentencia = "INSERT INTO tabla(id_persona, fecha, hora, tipo_acceso) VALUES (?, CURDATE(), CURTIME(), ?)";

                pst = connection .prepareStatement(sentencia);                
                pst.setInt(1, id_persona);
                pst.setInt(2, tipo_acceso);

            } catch (SQLException ex) {                
                System.out.println("No se ha podido insertar  en la Base de Datos.");                
            } finally{
                    try {
                    } catch (SQLException ex) {                    

Then I open the Task Manager and I see that my program in the Memory column every time I pulse increases the memory but never goes down, and there comes a time that consumes so much memory that the program stays stuck.

How can I solve that it does not raise the memory or that when the button is stopped the memory returns to more or less to its main value?

asked by Arkhan6 27.03.2017 в 11:49

1 answer


Answering your question:

  • There is nothing in the code that shows a "memory leak", usually a problem of excessive memory usage comes from recursive calls or because the objects that are instantiated are saved (for example, in Maps or Lists) way that can not be released. The mysql driver is so used that I do not think it's the cause.

    One point that is not clear to me is where the connection is created. Are you sure that you are only creating a connection during the entire execution of the program?

  • The rest of the points do not answer the question, they are rather advice about the code:

  • One of the advantages of the PreparedStatement is that, although they are slow to mount, if they are reused by changing the parameters they are quite fast. Since you leave the connection open (be careful, you can give timeout errors if you stop using it for a while!) Why not leave the PreparedStatement open too? In any case, it's just a matter of performance.

  • If there is no error, your code executes pst.close() twice. It should not be serious, but hey ...

  • If there is an error before or during line pst = connection .prepareStatement(sentencia) , the code in finally will throw a NullPointerException because no value has been assigned to pst .

  • If you use Java 7 or higher, you can use try-with-resources with PreparedStatement , and get rid of all the code to close it (and the two points above):

    try (PreparedStatement pst = conn.prepareStatement("....")) {
       // NO pst.close()
    } catch (SQLException sqle) {
  • With JDBC 4.0 drivers, it should no longer be necessary to do the Class.forName() . From Java Tutorial :


    In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName. [...]


    Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)

  • answered by 27.03.2017 / 12:09