0

我在 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");
    }
}
4

3 回答 3

0

根据http://www.bluej.org/help/faq.html#jvmargs页面

Windows:使用 bluej.defs 中的 bluej.windows.vm.args 属性

Linux/Unix/equivalent:编辑由安装程序创建的“bluej”shell脚本(在您安装BlueJ的目录中),并修改最后一行(启动BlueJ)。

更新

正如 Makato 所指出的,您面临的这个问题表明您的程序可能有问题,因为它似乎没有有效地使用内存。请发一个片段。

更新 您的代码看起来不错

于 2013-11-15T06:19:37.047 回答
0

我知道这已经很老了,但是没有正确的答案转到 bluej.defs 文件并添加

bluej.vm.args= -Xmx2G 或任意数量

注意 ^ windows 不存在

此外,您可能必须更改 bluej 正在使用的 JVM,因为默认值为 32 位,这可能会导致一些问题,您可以通过将 bluej 文件夹中的 jdk 文件夹重命名为 jdk-32 或其他任何内容来做到这一点,以便它会询问您什么 jdk你想在启动时使用选择一个 64 位的

于 2015-12-23T18:32:36.967 回答
-1

尝试使用 Windows 命令处理器运行具有更高内存的 java

javaw -Xmx2048 -Xms1024 -jar your_jar_file_name.jar

所以基本上,这xmx是你分配的最大内存,你xms是它应该使用的最小内存。

于 2013-11-15T06:23:39.050 回答