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();
}
}