[英]Out of Memory Heap Space
我正在编写一个代码,该代码将输出在三种可能性之间发生的一定数量对象的所有可能组合。 我的代码适用于较小的数字,例如 10,000,但我希望它能够达到 100,000。 每当我超过 10,000 时,我都会收到以下错误代码:java.lang.OutOfMemoryError: Java heap space。
有没有更有效的方法来存储这些信息,或者能够以某种方式规避错误代码? 我有下面的代码来显示我在说什么
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;
}
}
您不能将 100000 次迭代分成几块吗? 也许是 10000 次迭代的块? 并且对于每个块,将结果写入文件。这将摆脱 OOM 错误。
使用参数 -Xmx 提高最大内存,您可以在 java 类的运行配置中执行此操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.