Question:
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(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());
}
Answer:
heap
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.
OutOfMemoryError
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