Increase RAM memory timerTask

3

I am doing a query to a table in the SQL database, the problem is, I have to consult the data in real time, therefore I have created a task every 1 second, but it turns out that the RAM memory increases until that is full.

    public void sentenciaTiempoReal() {

    // Clase en la que está el código a ejecutar 
    timerTask = new TimerTask() {
        @Override
        public void run() {

            try {
                stmt = con.createStatement();
                rs = stmt.executeQuery(SQLTIEMPOREAL);
                muestraTiempoReal(rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("Contador "+contador++);
        }
    };
    // Aquí se pone en marcha el timer cada segundo. 
    timer.scheduleAtFixedRate(timerTask, 0, MILISEGUNDOS);     
}

private void muestraTiempoReal(ResultSet rs) throws ParseException {
    int estado;
    try {
        while (rs.next()) {
            estado = rs.getInt(2);
            jlRobot.setText("Robot: " + rs.getString(3));
            //fecha y hora 
            jlFechaUltAct.setText("Última actualización: " + rs.getTimestamp(4));
            Calendar calfFinal = Calendar.getInstance();
            calfFinal.setTime(rs.getTimestamp(4));
            String fechaFinal = String.valueOf(rs.getTimestamp(4));

            jlTiempoEstado.setText(operaciones.entreDosFechas(fechaFinal, operaciones.getFechaActual()));

            //huellas
            String huellasActivas = String.valueOf(operaciones.decimalABinario(rs.getInt(11)));
            jlHuellasActivas.setText("Huellas activas: " + String.valueOf(operaciones.contadorHuellas(huellasActivas)));

            //huellas
            jlBuenas.setText("BUENAS: " + rs.getDouble(5));
            jlProcesadas.setText("PROCESADAS: " + rs.getDouble(6));
            jlMalas.setText("MALAS: " + rs.getDouble(7));
            jlCiclo.setText("Velocidad de la máquina: " + rs.getDouble(8) + "ms");
            if (estado == 1) {
                jbEstado.setBackground(Color.green);
                jbEstado.setText("MARCHA");
            }
            if (estado == 2) {
                jbEstado.setBackground(Color.RED);
                jbEstado.setText("PARO O STAND-BY");
            }
            //evento zona de fallo cesar
            eventoFalloVibrador(rs.getInt(9));
        }

    } catch (SQLException ex) {
        Logger.getLogger(Ventana.class
                .getName()).log(Level.SEVERE, null, ex);
    }
}
    
asked by R.Priego 13.10.2016 в 23:23
source

2 answers

0

How are you using SQL Server I recommend reading about SQL Server Query Notification link

It also reviews Apache ActiveMQ is free and can help you in this case.

    
answered by 19.10.2016 / 18:08
source
0

The main problem is that you are leaving the ResultSet open, so every time you ask for one, you create a new one and the previous one stays in memory linked to the connection.

Many optimizations could be made but the basics would be to use a PreparedStatement with or without parameters, depending on whether SQLTIEMPOREAL is static or changing parameters, and closing the ResultSet to free resources.

public void sentenciaTiempoReal() {

     PreparedStatement stmt = con.prepareStatement(SQLTIEMPOREAL,
            ResultSet.TYPE_FORWARD_ONLY,
            ResultSet.CONCUR_READ_ONLY);

    // Clase en la que está el código a ejecutar 
    timerTask = new TimerTask() {
        @Override
        public void run() {
            try {
                ResultSet rs = stmt.executeQuery();
                muestraTiempoReal(rs);
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("Contador " + contador++);
        }
    };
    timer.scheduleAtFixedRate(timerTask, 0, MILISEGUNDOS);     
}

Needless to say, this example is not perfect and as I said before, you can optimize much more. You can improve the process, the management of the connection, the SQL, ...

But at least it would be to keep the connection (object with), use the preparedStatement so as not to create the query all the time and, above all, close the ResultSet to release the data from the query made.

    
answered by 20.10.2016 в 14:43