java – understand error message


I am unable to 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(
at java.util.Arrays.copyOf(
at java.util.ArrayList.grow(
at java.util.ArrayList.ensureExplicitCapacity(
at java.util.ArrayList.ensureCapacityInternal(
at java.util.ArrayList.add(
at so.SO.main(

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) {
                        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]));

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

                } else {


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

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

    } catch (IOException e) {

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




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

Only objects are allocated in the heap . Methods and other stops are stored elsewhere.

The heap is divided into two regions: Nursery and Old Space .

  • Nursery : Region where new objects are allocated

  • Old Space : Region where objects that have some time to live are allocated

How it works?

When the Nursery starts to fill, a kind of "transition" of objects between one region and another is made. This transition is called the Young Collection , where the objects initially allocated in Nursery (which are already sometime old) go to the Old Space region.

When the Old Space region starts to fill up, a collection called the Old Collection is made, where the "old" objects actually start to be removed from memory.


I am unable to understand this error message. Does anyone understand and can you help me?

This message informs you that all space in the Heap has been used. The Garbage Collector was unable to free the amount of memory necessary for the application to continue running in time.

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

How to solve?

This is directly related to your implementation.

No code was posted, so guys SOpt can't help you make a better/less "costly" implementation.

Post your code so we can help.

Common OutOfMemoryError Scenarios

Here are some common scenarios where OutOfMemoryError might occur:

  • Repeating loops that create lots of new objects

  • Reading and/or writing to files storing a lot of information in memory

  • Bring a lot of information from the bank ("dumb" paging is an example)

  • Keep references to objects unnecessarily

Among several other possibilities.

References: Heap and OutOfMemoryError

Scroll to Top