Understand error message

4

I can not understand this error message. Does anyone understand and can you help me?

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3210)
at java.util.Arrays.copyOf(Arrays.java:3181)
at java.util.ArrayList.grow(ArrayList.java:261)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
at java.util.ArrayList.add(ArrayList.java:458)
at so.SO.main(SO.java:100)

My code

try {

        try (FileReader arq = new FileReader(arquivo)) {
            BufferedReader lerArq = new BufferedReader(arq);

            String linha = lerArq.readLine();

            num_processos = Integer.parseInt(linha);

            String array[] = new String[3];  // array criado para determinar a quantidade de parãmetros a serem armazenados.

            while (linha != null) {         // Condição de saída do arquivo pois quando acabam as linhas o valor é null.

                linha = lerArq.readLine();

                if (linha != null) //Condição usada para verificar se está em uma linha vazia uma vez que somente o while ainda dava alguns erros.
                {
                    quantidade_linha++; // Contador de linhas do arquivo "txt".

                    cont_caracter = 0;  // Contador de caracteres.

                    if (quantidade_linha == 2) {
                        num_ciclos = Integer.parseInt(linha);

                    }

                    if (quantidade_linha > 2) {
                        PID++;
                        temporario = new Processos();  // instanciamento do processo temporário para que a cada linha seja registrado um novo.
                        temporario.inicializa(PID);   // Método contido na classe de Processos para inicializar com "0" os valores do mesmo.

                        while (cont_caracter < linha.length()) {      // Condição para chegada ao fim da linha.

                            array = linha.split(",");           // Caracter usado para separamento usando o método split.

                            // Atribuir os valores ao processo temporário.
                            temporario.num_ciclos = (Integer.parseInt(array[0]));
                            temporario.entrada_saida = (Integer.parseInt(array[1]));
                            temporario.prioridade = (Integer.parseInt(array[2]));

                            cont_caracter++;
                        }
                        // Condição para determinar em qual lista o processo entratrá dependendo da sua prioridade.
                        if (temporario.prioridade == 0) {
                            prioridade0.add(temporario);
                        } else if (temporario.prioridade == 1) {
                            prioridade1.add(temporario);
                        } else if (temporario.prioridade == 2) {
                            prioridade2.add(temporario);
                        }

                    }
                } else {
                    break;
                }

            }

            while (!fim) {
                for (i = 0; i < prioridade2.size(); i++) {
                    while (prioridade2.get(i).num_ciclos > 0) {
                        pronto.add(prioridade2.get(i));
                    }
                }
                for (i = 0; i < prioridade1.size(); i++) {
                    while (prioridade1.get(i).num_ciclos > 0) {
                        pronto.add(prioridade1.get(i));
                    }
                }
                for (i = 0; i < prioridade0.size(); i++) {
                    while (prioridade0.get(i).num_ciclos > 0) {
                        pronto.add(prioridade0.get(i));
                    }
                }

                fim = true;
            }
            System.out.println("FIM DA EXECUÇÃO!");
        }

    } catch (IOException e) {

        System.err.printf("Erro na abertura do arquivo: %s.\n",
                e.getMessage());

    }
    
asked by anonymous 28.06.2017 / 16:50

2 answers

6

Heap

Heap is the place (memory space) where objects created in Java are allocated.

In heap only objects are allocated. Methods and other stops are stored elsewhere.

Heap is divided into two regions: Nursery and Old Space .

  • Nursery : Region where new objects are allocated

  • Old Space : Region where objects that already have some life span are allocated

How does it work?

When Nursery begins to fill, a sort of "transition" of objects is made between one region and the other. This transition is called the Young Collection , where objects initially allocated to Nursery in>.

When the Old Space region begins to fill, a collection is called Old Collection , where "old" objects are actually removed from memory.

OutOfMemoryError

  

I can not understand this error message. Does anyone understand and can you help me?

This message informs that all the space in Heap has been used. The Garbage Collector failed to release the amount of memory needed to continue running the application on time.

It is no longer possible to move Nursery objects to Old Space or remove Old Space

How to solve?

This is directly related to your implementation.

No code has been posted, so people at SOpt can not help you make a better / less "costly" implementation.

Post your code so we can help.

Common scenarios of OutOfMemoryError

Here are some common scenarios where OutOfMemoryError can occur:

  • Repeat loops that create many new objects

  • Read and / or write to files storing too much information in memory

  • Bringing a lot of information from the database ("dumb" pagination is an example)

  • Keep references to objects unnecessarily

Among many other possibilities.

References: Heap and OutOfMemoryError

    
28.06.2017 / 18:09
6

This is because the default JVM configuration (if you have not changed any parameters in the installation) rotates around 16 MB.

But there is no way to give you a conclusive answer because I do not know what caused your application to use all the allocated memory space, you could be running an infinite loop, filling a buffer vector, among many other options if your application is really large and uses more memory, you can reconfigure this volume in your JVM.

To reconfigure the volume of memory used by the JVM at runtime we have two commands:

  • -Xms : which defines the minimum amount of Heap memory for the JVM;
  • -Xmx : which defines the maximum amount of Heap memory for the JVM.

We can use it to run: Java –Xms256M –Xmx1024M –jar seuApp.jar

Link to memory configuration in the JVM, very explanatory by the sign of luizricardo.org

If you use NetBeans you can change these values through the Project Properties, in Build > Run > VM Options

    
28.06.2017 / 17:00