我用Bluej编写了一个java程序,并在较小的值(100000)下运行。但对于更大的界限(1000000),我使用的是java.lang.OutOfMemoryError: Java heap space。我如何在Bluej中解决它?提前谢谢。
import java.io.*;
import java.util.*;
class prob
{
private static final int N = 1000000;//5000000
private static final int h = Math.min(N, (int)(Math.cbrt(0.5*N*N)));;
private static byte[][] small;
private static int[] smallSums;
private static int[] smallCounts;
private static int periodCount;
private static int periodSum;
private static void recursiveInit(int x, int y, int steps, int h)
{
if (x <= h)
{
for (int z = x + y; z <= 2*h; z += x)
recursiveInit(z, x, steps + 1, h);
}
else if (x <= h + y)
{
small[y][x - h - 1] = (byte)steps;
}
}
private static long recurseRule(int a, int b, int c, int d, int steps, int limit, int y)
{
int i = c;
int j = d;
long sum = 0;
for (;;)
{
i += a;
j += b;
if (i*(h + 1) + j*y > limit) break;
int xmax = (limit - j*y)/i - (h + 1);
int k = xmax%y;
long cnt = smallCounts[k] + (xmax/y)*periodCount;
long s = smallSums [k] + (xmax/y)*periodSum;
sum += cnt*steps + 2*s + recurseRule(i, j, a, b, steps + 2, limit, y);
}
return sum;
}
public static void main(String[] args)
{
double start = System.currentTimeMillis();
smallCounts = new int[h];
smallSums = new int[h];
small = new byte[h + 1][];
for (int y = 1; y <= h; ++y) small[y] = new byte[y];
for (int x = 2; x <= 2*h; ++x) recursiveInit(x, 1, 1, h);
long sum = N;
for (int y = 1; y <= h && y <= N; ++y)
{
smallSums[0] = small[y][0];
smallCounts[0] = 0;
if (small[y][0] != 0) ++smallCounts[0];
for (int i = 1; i < y; ++i)
{
smallSums[i] = smallSums[i - 1] + small[y][i];
smallCounts[i] = smallCounts[i - 1];
if (small[y][i] != 0) ++smallCounts[i];
}
periodCount = smallCounts[y - 1];
periodSum = smallSums[y - 1];
int f = (h + 1)/y + 1;
for (int gmax = N/y; gmax > 0;)
{
int r = N/gmax;
int gmin = N/(r + 1);
int i1 = (y + y*f) - (h + 1);
int i2 = (r + y*f) - (h + 1);
int j1 = i1%y;
int j2 = i2%y;
int k = i2/y - i1/y;
int s = smallSums [j2] - smallSums [j1] + k*periodSum;
int c = smallCounts[j2] - smallCounts[j1] + k*periodCount;
sum += (gmax - gmin)*(2L*s + c + recurseRule(1, 0, 0, 1, 3, r, y));
gmax = gmin;
}
}
System.out.println("The sum is "+sum);
double end = System.currentTimeMillis();
System.out.println("Time elapsed : "+(end-start)/1000d+" seconds");
}
}发布于 2013-11-15 14:19:37
根据http://www.bluej.org/help/faq.html#jvmargs页面
Windows:在bluej.defs中使用bluej.windows.vm.args属性
Linux/Unix/等效物:编辑由安装程序创建的"bluej“shell脚本(在安装BlueJ的目录中),并修改最后一行(启动BlueJ)。
更新
正如Makato所指出的,您面临的这个问题表明您的程序可能存在问题,因为它似乎没有有效地使用内存。请发布一个片段。
更新您的代码看起来正常
发布于 2015-12-24 02:32:37
我知道这很古老,但是没有正确的答案,请转到bluej.defs文件并添加
bluej.vm.args= -Xmx2G或任何金额
注意:^ windows不在那里
发布于 2013-11-15 14:23:39
尝试使用Windows命令处理器运行具有更高内存的java
javaw -Xmx2048 -Xms1024 -jar your_jar_file_name.jar
因此,基本上,xmx是您分配的最大内存,而xms是它应该使用的最小内存。
https://stackoverflow.com/questions/19994692
复制相似问题