Memory consumption in java

1

Good morning, people.

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

The application is working correctly in the schedules that have been programmed, however in the time that it stops, it is consuming a lot of memory (around 1300 MB)

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

If someone has an idea of what can be or some correction of the problem, follow the code below.

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{

        }
    }
}
    
asked by anonymous 03.08.2016 / 17:08

2 answers

4

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

//Pausa por 10 segundos, por exemplo
Thread.sleep(10000);
    
03.08.2016 / 17:41
2

You are redoing statements within the loop. Step out of 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 are not closing 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 .

    
03.08.2016 / 18:43