Memory consumption in java

Asked

Viewed 406 times

1

Good morning Personal.

I made a java application that at certain times it executes a database query and generates a text file.

The application is working right in the schedules that were programmed, however in the time it stands still it is consuming a lot of memory (around 1300 MB)

I think this is not normal because some desktop and wev applications that would have to have a higher consumption are not even close to what this application is consuming.

If anyone has an idea of what may be or any fix of the problem follows below the code.

private static String fileStream = "C:\\Users\\genesys\\Documents\\RM\\arq.txt";
//private static String fileStream = "C:\\Users\\Vostro-3300\\Desktop\\A comparar\\arq.txt";
public static void main(String[] args) throws IOException {
    boolean fim = false;

    while(fim == false){

        Calendar calendario = new GregorianCalendar();
        Date dt = calendario.getInstance().getTime();
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        String dtF = df.format(dt);

        if(dtF.equals("06:00:00")||dtF.equals("06:00:01")||dtF.equals("06:00:02")||dtF.equals("06:00:03")){
            try{
                // função para envio de email
                }catch(Exception ex){
                    ex.printStackTrace();
                }       
        }

        if(dtF.equals("18:00:01") ||dtF.equals("18:00:02")||dtF.equals("18:00:03")||dtF.equals("11:25:00")||dtF.equals("11:25:01")||dtF.equals("11:25:02")){
            try{
                Conecta c = new Conecta();
                ArrayList<Dados> lista = c.listar();

                FileWriter arq = new FileWriter(fileStream);
                PrintWriter print = new PrintWriter(arq);

                print.println("CCODCOLIGADA;CODSECAO;DESCRICAO;NROCENCUSTOCONT;NOME;CODFUNCAO;NOME1;CHAPA;NOME2;DTNASCIMENTO;JORNADA;SALARIO;SEXO;DATAADMISSAO;DATADEMISSAO;STATUS;TIPOCONTRATO;DESCRICAOFUNCAO;GRAUINSTRUCAO;CPF;PISPASEP;RG;CTPS;ESTADOCIVIL;ENDERECO;BAIRRO;CIDADE;ESTADO;CEP;TELEFONE;EMAIL;TIPOADMISSAO");

                for(Dados d: lista){
                    print.println(d.getCodLigada()+";"+d.getCodSecao()+";"+d.getDescricao()+";"+d.getNroCenCustoCont()+";"+d.getNomeCent()+";"+d.getCodFuncao()+";"+d.getNomeFunc()+";"+d.getChapa()+";"+d.getNome()+";"+d.getDtNasc()+";"+d.getJornada()+";"+d.getSalario()+";"+d.getSexo()+";"+d.dtAdmissao+";"+d.getDtDemisao()+";"+d.getStatus()+";"+d.getContratoTipo()+";"+d.getCargoDesc()+";"+d.getGrauInstrucao()+";"+d.getCpf()+";"+d.getPisPasep()+";"+d.getRg()+";"+d.getNmrCtps()+";"+d.getEstadoCivil()+";"+d.getEndreco()+";"+d.getBairro()+";"+d.getCidade()+";"+d.getEstado()+";"+d.getCep()+";"+d.getTelefone()+";"+d.getEmail()+";"+d.getTipoAdmissao());
                }

                arq.close();
            }
            catch(Exception e){
                try{
                    // função para envio de email
                    }catch(Exception ex){
                        ex.printStackTrace();
                    }       
            }
        }else{

        }
    }
}
  • 2

    Isn’t it simpler to schedule a task in the OS to run this program at certain times? Depending on the time, you initialize the program with a different argument (and you can pick it up from args).

  • @Nan may be an option but in the future I will need some information that this tool will produce at random times.

2 answers

4


The problem is that your application never stops. The suggestion given by Renan is the best. But, the problem of memory consumption can be solved with a sleep.

//Pausa por 10 segundos, por exemplo
Thread.sleep(10000);
  • worked well with Thread.Sleep(); but it has a one-hour limit. if I want for example that it waits 10 hours I would need to make a loop in it or there is another way?

  • 1

    Yes, you will have to use Threads and within each Thread you will have to use Sleep with the result of the waiting time (in milliseconds) of the subtraction of the time you want to run with the current time.

  • 1

    I don’t recommend doing too many chained sleeps, this will make the machine busy waiting, that is, you won’t be able to interrupt the waiting. If you want to make a wait that can be interrupted, use Threads in operations and other resources like Syncronize, etc. Look at this article> http://www.devmedia.com.br/java-threads-utilizando-wait-notify-e-notifyall/29545

  • @Celsomarigojr legal I will take a look at this material. I managed to solve with the implementation of Thread.sleep(); thanks for the help

2

You are remaking statements within the loop. Go to declare outside the loop:

final Calendar calendario = new GregorianCalendar();
final Date dt = calendario.getInstance().getTime();
final SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
final String dtF = df.format(dt);

while (!fim) {
    // ...
}

You’re not closing the printWriter.

try {
    Conecta c = new Conecta();
    List<Dados> lista = c.listar();

    PrintWriter print;

    try {
        print = new PrintWriter(new FileWriter(fileStream));

        for (Dados d : lista) {
            // ...
        }
    } finally {
        print.close();
    }
} catch(Exception e) {
    try {
        // função para envio de email
    } catch(Exception ex) {
        ex.printStackTrace();
    }
}

Implement the idea of our colleague Celso, use sleep.

  • had not touched me from print.close(). I changed the code to declare the variables outside the loop but even that the gain was very little. Know if there is or has how to measure how much is spending each process?

  • I’ve heard of Apache Jmeter, but I’ve never touched it. http://jmeter.apache.org/

  • Did you ever test if the neck would not be in your class Conecta? Try to comment on the line Conecta c = new Conecta(); and their calls to see what happens to memory usage.

  • On the performance, read this: http://www.vogella.com/tutorials/JavaPerformance/article.html it talks a little about Visualvm, native to java sdk, which can be used to test performance.

  • @Victort. got I’ll get on this tool and referring to class Conecta already tested and it consumes very little. After implementing the Thread.sleep(); has decreased enough thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.