How to free java memory in a thread

1

I am working on a Java application, it must transfer information from one database to another in a certain time interval. Currently works with an infinite cycle within a thread, because the user needs to open the application and it does not stop working all day. It happens that while it is running, the application accumulates a lot of memory, after which the message appears

[java.lang.OutOfMemoryError]

If I'm not mistaken, the memory is released when the cycle ends, but in this case it's infinite, what can be done?

Here is the class

package Model;

import Control.Controller;
import static java.lang.Thread.sleep;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Ciclo implements Runnable {

Controller myController;
private boolean iterar;
private String web_service;
private String token;
private String pc;
private int segundos = 10000;

public void setSegundos(int segundos){
    this.segundos = segundos;
}

@Override
public void run() {
    int i = 0;
    while (iterar) {
        try {


            if (!myController.getConexionWebService(web_service, token, pc)){
            } else if (!myController.getConexionMysql()){
            } else if (!myController.getJsonFileExist()){
            }else{

                myController.proceso1();

            }

            if (!myController.getConexionWebService(web_service, token, pc)){
            } else if (!myController.getConexionMysql()){
            } else if (!myController.getJsonFileExist()){
            }else{

                myController.proceso2();

            }
            sleep(segundos);
        } catch (Exception ex) {
            Logger.getLogger(Ciclo.class.getName()).log(Level.SEVERE, null, ex);
            myController.setWriteError(ex.getMessage());
        }
    }

    myController.setReanudarTrue();
    return;
}

public void setController(Controller myController) {
    this.myController = myController;
}

public void iniciar(String web_service, String token, String pc) {
    this.web_service = web_service;
    this.token = token;
    this.pc = pc;
    iterar = true;
    new Thread(this).start();
}

public void pausar() {
    iterar = false;
}

public void reanudar() {
    iterar = true;
    new Thread(this).start();
}



}
    
asked by Sagara 04.07.2018 в 23:51
source

1 answer

2

What can be is the loss of memory in one of your processes caused by not closing a resource, either a PreparedStatement, ResultSet, sql.Connection, http.Connection among many things that you can have as memory loss.

You may also be getting too heavy data from the database, or from the webservice and want to pass it to byte array, so I recommend you use Stream for blobs, or even for oversized strings.

I recommend using a profile of the virtual machine to see which object is the one that is accumulating, so you can give us more information.

link

If you use Java 7 or higher use the try-catch-resources link

    
answered by 05.07.2018 / 20:00
source