Java错误" main" java.lang.OutOfMemoryError:Java堆空间

时间:2017-10-28 05:21:02

标签: java edit-distance

所以我在java中编写了以下主要函数来计算1000个随机生成的长度为10,20,50和100的editdistancee。它对于长度为10 n 20的运行正常,但对于长度为50,它给出了这个错误。 "线程中的异常" main" java.lang.OutOfMemoryError:Java堆空间"。我很困惑该怎么做。任何帮助将不胜感激。

for (j=0; j< numoftimes; j++) {
            for (int i = 0; i < len2; i++) {
                s5r += (char) ( 'a' + r.nextInt(26));
                s6r += (char) ( 'a' + r.nextInt(26));
            }
            starttime2 = System.nanoTime();
            int distance2 = editDistance(s5r,s6r); 
            endtime2 = System.nanoTime() - starttime2; 
            }

            avg_CPUtime2 = (endtime2/numoftimes);
            System.out.println("Average CPU time in nanoseconds for 1000 
               pair of random words of length "+len2+" : "+avg_CPUtime2);


            int len3 = 100;
            long starttime3 = 0;
            long endtime3 = 0;
            long avg_CPUtime3 = 0;
            String s7r = "";
            String s8r = "";
            for (j=0; j< numoftimes; j++) {
            for (int i = 0; i < len3; i++) {
                s7r += (char) ( 'a' + r.nextInt(26));
                s8r += (char) ( 'a' + r.nextInt(26));
            }
            starttime3 = System.nanoTime();
            int distance3 = editDistance(s7r,s8r); 
            endtime3 = System.nanoTime() - starttime3; 
            }

            avg_CPUtime3 = (endtime3/numoftimes);
            System.out.println("Average CPU time in nanoseconds for 1000 
              pair of random words of length "+len3+" : "+avg_CPUtime3);

out out this this

Average CPU time in nanoseconds for 1000 pair of random words of length 10 : 
 674163
  Average CPU time in nanoseconds for 1000 pair of random words of length 20 
     : 3128792
   Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
 at algorithmDesign.Sequences.editDistance(Sequences.java:12)
 at algorithmDesign.Sequences.main(Sequences.java:116)

1 个答案:

答案 0 :(得分:0)

你必须增加java堆空间

在Run-&gt; Run Configuration中找到您正在运行的类的名称,选择它,单击Arguments选项卡,然后添加:

-Xms512M -Xmx1024M

到VM Arguments部分