简体   繁体   中英

Out of Memory Heap Space

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;
    }
}

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.

使用参数 -Xmx 提高最大内存,您可以在 java 类的运行配置中执行此操作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM