Java error "main" java.lang.OutOfMemoryError: Java heap space

Java error "main" java.lang.OutOfMemoryError: Java heap space

所以我在 java 中编写了以下主要函数来计算 1000 对随机生成的长度为 10、20、50 和 100 的编辑距离。运行 对于长度 10 n 很好20 但长度为 50 时会出现此错误。 "Exception in thread "main" java.lang.OutOfMemoryError: Java 堆 space"。我很困惑该怎么办。任何帮助将不胜感激。

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

出来的是这个

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)

你必须增加 java 堆 space

在运行->运行Configuration中找到class你已经运行的Name,select它,点击Arguments选项卡然后添加:

-Xms512M -Xmx1024M

到 VM 参数部分