Home > OS >  Out of Memory Heap Space
Out of Memory Heap Space

Time:06-08

I am working on a code that will output all the possible combinations of a certain amount of objects that will occur between three possibilities. My code is working for smaller numbers like 10,000 but I want it to be able to go up to 100,000. Anytime I go above 10,000 I get the following error code: java.lang.OutOfMemoryError: Java heap space.

Is there a more efficient way to store this information, or be able to circumvent the error code somehow? I have the code below to show what I am talking about

public static double Oxygen[][];
public static int OxygenPermutationRows;

public void OxygenCalculations(){
    
    Scanner reader = new Scanner(System.in);
    System.out.print("Oxygen Number: ");
    int oxygenNumber = reader.nextInt();
    System.out.println();
    int oxygenIsotopes = 3;

    OxygenPermutationRows = 0;

    //Number of Feesable Permutations
    if(oxygenNumber % 2 == 0)
    {
        OxygenPermutationRows = (1   oxygenNumber) * ((oxygenNumber / 2)   1);
    }
    else
    {
        OxygenPermutationRows = (1   oxygenNumber) * (int)Math.floor(oxygenNumber / 2)   (int)Math.ceil(oxygenNumber / 2)   2   oxygenNumber;
    }

    int [][] Permutations = new int[OxygenPermutationRows][oxygenIsotopes];

    int counterrow = 0;
    int k;
    for (int f = 0; f <= oxygenNumber; f  )
    {
        for (int j = 0; j <= oxygenNumber; j  )
        {
                k = oxygenNumber - j - f;
                Permutations[counterrow][0] = f;
                Permutations[counterrow][1] = j;
                Permutations[counterrow][2] = k;
                counterrow  ;
                if(f j == oxygenNumber)
                {
                    j = oxygenNumber   10;
                }
            }   
    }

//TO CHECK PERMUTATION ARRAY VALUES
System.out.println("PERMUTATION ARRAY =======================================");
for (int i = 0; i < OxygenPermutationRows; i  ) {
  System.out.println();
  for (int j = 0; j < 3; j  ) {
  System.out.print(Permutations[i][j]   " ");
  }
}

    public double[][] returnOxygen()
    {
        return Oxygen;
    }
    
    public double returnOxygenRows()
    {
        return OxygenPermutationRows;
    }
}

CodePudding user response:

Couldn't you split the 100000 iterations in chunks? Maybe chunks of 10000 iterations? And for every chunk, write the result in a file.This will get rid of the OOM error.

CodePudding user response:

Raise max memory with the parameter -Xmx, you can do this in the run configuration for the java class

  • Related