java.lang.OutOfMemoryError

2 respostas
A

Olá pessoal,

Estou com problemas em meu código JAVA, quando retorno um ArrayList (individuo) de 100 posições ele dá o seguinte erro:

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.addAll(ArrayList.java:579)

at ag_pmpc.AG_PMPC_Principal.ag(AG_PMPC_Principal.java:139)

at ag_pmpc.AG_PMPC.main(AG_PMPC.java:17)

Java Result: 1

Se eu retiro o return(individuo) e coloco return(Individuo) (com i maiúsculo) ele não dá erro, porém o indivíduo com i maiúsculo é o mesmo que a classe está recebendo.

O que faço nessa classe é receber um indivíduo (ArrayList tamanho 100) e “tratá-lo”. O indivíduo que tenho que retornar é o indivíduo “tratado”.

Segue abaixo o código:

package ag_pmpc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class AG_PMPC_Atendido_Capacidade {
    
public static int troca = 0;
public static int aux_numero_medianas = 0;

public static long aux_maior_demanda=0;
public static long aux_menor_demanda=1000000;

public static int aux_indice_maior_demanda =0;
public static int aux_indice_menor_demanda =0;

public static int aux_mediana_maior_demanda=0;
public static int aux_mediana_menor_demanda=0;

public static long aux_diferenca_maior_demanda = 0;
public static long aux_diferenca_menor_demanda = 0;

public static int aux_tamanho_instancia = 0;

public static float med = 0;
public static long soma_demanda =0;
public static long xdemanda = 0;
public static float xcapacidade = 0;
public static int aux_excesso =0;
public static float aux_xcapacidade = 0;

//public static int[] aux_capacidade = new int[aux_tamanho_instancia]; 
public static ArrayList<Long> aux_demandas_clusters = new ArrayList<>();
public static ArrayList<Integer> aux_medianas = new ArrayList<>();
public static ArrayList<Integer> aux_capacity = new ArrayList<>();
public static ArrayList<Integer> aux_demandas = new ArrayList<>();
public static ArrayList<Integer> individuo = new ArrayList<>();

@SuppressWarnings("empty-statement")
    public static ArrayList<Integer> atendido_capacidade(ArrayList<Integer> Individuo, int[]demanda, int tamanho_instancia, ArrayList<ArrayList<Float>> matriz, int[]capacidade, int numero_medianas, ArrayList<Integer>medianas){  
   // System.out.println("CHEGANDO EM  Atendido Capacitada ");

       
    
    float fo =0;
//        long xdemanda = 0;
    //    float xcapacidade = 0;
        //long soma_demanda = 0;
       // float med = 0;
        int individuo_sem_melhora = 0;
        long menor_valor =1000;
        long valor =0;
        int indice =0;
        int med_aux =0;
        int ultimo_elemento = 0;
        int parada = 0;
       // public static int nomeDaVariavel;
        long diferenca_maior_demanda =0;
        long diferenca_menor_demanda =0;
        long maior_demanda=0;
        long menor_demanda= 1000000;
        int indice_maior_demanda =0;
        int indice_menor_demanda =0;
        int mediana_maior_demanda=0;
        int mediana_menor_demanda=0;
      //  int aux_excesso = numero_medianas;
        Random random = new Random();
        Random rnd = new Random();
        
        ArrayList<Long> demandas_clusters = new ArrayList<>();
        ArrayList<ArrayList> demandas_ajustadas = new ArrayList<ArrayList>();
        ArrayList<Integer> demandas = new ArrayList<>();
        ArrayList<Integer> capacity = new ArrayList<>(tamanho_instancia);
        
        aux_indice_maior_demanda =    indice_maior_demanda;
        aux_indice_menor_demanda =    indice_menor_demanda;
        
        aux_numero_medianas = numero_medianas; 
        aux_excesso = numero_medianas;
        aux_mediana_maior_demanda = mediana_maior_demanda;
        aux_diferenca_maior_demanda = diferenca_maior_demanda;
        aux_tamanho_instancia = tamanho_instancia;
        aux_maior_demanda = maior_demanda;
        
        aux_medianas.addAll(medianas);
        aux_demandas.addAll(demandas);
        individuo.addAll(Individuo);
        
        for (int j=0; j< tamanho_instancia; j++){
            capacity.add(0);
        }
        aux_xcapacidade = capacity.get(1);
        for (int j=0; j< tamanho_instancia; j++){
            demandas.add(0);
        }
        for (int j=0; j< tamanho_instancia; j++){
            capacity.set(j, capacidade[j]);
        }
        for (int j=0; j< tamanho_instancia; j++){
            demandas.set(j, demanda[j]);
        }
        aux_demandas.addAll(demandas);
        aux_capacity.addAll(capacity);

        for (int j=0; j< tamanho_instancia; j++){
        
            xcapacidade = capacidade[j];
        }
        aux_xcapacidade = xcapacidade;
        
        while (aux_excesso > 0)   {
            
          if (individuo_sem_melhora < 50 ){

             Soma_demandas(); // Função que soma as demandas dos indivíduos.

            Avalia_maior_demanda(); // Função que avalia o cluster de MAIOR demanda.
            
            Avalia_menor_demanda(); //Função que avalia o cluster de MENOR demanda.

            
                for (int j=0; j< tamanho_instancia; j++){
                    if (individuo.get(j)== aux_mediana_maior_demanda) {
                        if (aux_demandas.get(j) <= aux_diferenca_menor_demanda){
                            if (individuo.get(j)!=(j+1))  { // verifica se o vértice é diferente da mediana.
                                individuo.set(j, aux_mediana_menor_demanda);
                                
                                aux_demandas_clusters.clear();
                                Soma_demandas();
                                Avalia_maior_demanda();
                                Avalia_menor_demanda(); 
                                j=0;
                                if (aux_diferenca_maior_demanda <=0){
                                    System.out.println(aux_demandas_clusters); //MODIFICADO**
                                    break;
                                }
                            }
                        }
                    }
                }
                
            Avalia_excesso(); // Avalia quantos cluster estão "estourados".
            
            aux_demandas_clusters.clear();

          individuo_sem_melhora ++;

          } 
          
          
        } 


    return  individuo;   
    }

    public static void Soma_demandas(){
        
        for (int i=0; i < aux_numero_medianas; i++){
            med = aux_medianas.get(i); // variável med recebe a mediana.
            soma_demanda =0; 
                
            for (int j=0; j < aux_tamanho_instancia; j++){
                if(individuo.get(j) == med ){
                    xdemanda = aux_demandas.get(j);
                    soma_demanda = soma_demanda + xdemanda;
                }
                else{
                    continue;
                }
            }
            aux_demandas_clusters.add(soma_demanda);
        }
    }
    
    public static void Avalia_maior_demanda(){
         
      //  System.out.println("Diferença da MAIOR demanda");
        aux_maior_demanda =0;
        for (int j=0; j< aux_numero_medianas; j++){
            if (aux_demandas_clusters.get(j) > aux_maior_demanda) {
                aux_maior_demanda = aux_demandas_clusters.get(j); // Armazena a maior demanda da mediana
                aux_indice_maior_demanda = j;
                aux_mediana_maior_demanda = aux_medianas.get(aux_indice_maior_demanda);
            }
            aux_diferenca_maior_demanda = aux_maior_demanda - aux_capacity.get(j);
        }
    }
    
    public static void Avalia_menor_demanda(){
      //  System.out.println("Diferença da MENOR demanda");
        aux_menor_demanda =1000000;
         for (int j=0; j< aux_numero_medianas; j++){
            if (aux_demandas_clusters.get(j) < aux_menor_demanda) {
                aux_menor_demanda = aux_demandas_clusters.get(j); // Armazena a menor demanda da mediana
                aux_indice_menor_demanda = j;
                aux_mediana_menor_demanda = aux_medianas.get(aux_indice_menor_demanda);
            }
            aux_diferenca_menor_demanda = aux_capacity.get(j) - aux_menor_demanda;
        }    
    }

    public static void Avalia_excesso(){           
 
        for (int j=0; j< aux_numero_medianas; j++){
            if (aux_demandas_clusters.get(j) <= aux_xcapacidade){
                aux_excesso = aux_excesso - 1;
            }
        }
    }
 }

Alguém pode me ajudar nessa?
Desde já obrigado!

2 Respostas

j-menezes

Precisa analisar bem o codigo pra saber o motivo real do estouro da memoria.
Mas voce pode adequar o -Xms32M -Xmx1024M como parametro de chamada do java.

staroski

Seu StackTrace mostra que o erro está acontecendo no método ag do arquivo AG_PMPC_Principal.java, precisamente na linha 139.

No entanto, o código que você postou é outro.

Atenção: Seu código está muito estranho, você está declarando tudo como static, todos os métodos são void e parece que tem muitas variáveis desnecessárias.

Criado 20 de julho de 2017
Ultima resposta 21 de jul. de 2017
Respostas 2
Participantes 3