如何修复我的系统内存不足异常?

How to fix my system out of memory exception?

我正在尝试创建一个程序,它将连续 6 次给出 1 到 55 之间的每个数字。

也就是;

1-17-25-45-49-51

55-54-53-52-51-50

注意,两个数字不能连续出现。

我已经在较小的规模上测试了这个程序,到目前为止它按预期运行。但是,如果我 运行 满刻度,如下面发布的代码所示,它 运行 变成 "out of memory exception"。我的猜测是,这是因为在连续构建 ArrayList 和文件时内存必须容纳如此大的数字。

我该如何解决这个问题?

class MainClass

{

    public static void Main (string[] args)

    {

        ArrayList newWords= new ArrayList();
        System.Console.WriteLine ("Please enter word to prepend ");
        int wordCount = 0;
        string numbers;
        string word = Console.ReadLine ();
        string word2 = word;
        string separator = "-";

        System.Console.WriteLine ("Please enter location of where you would like the new text file to be saved: ");
        string newFileDestination = Console.ReadLine ();

        TextWriter writeToNewFile = new StreamWriter (newFileDestination);

        for(int a = 1; a < 56; a++){
            for(int b = 1; b < 56; b++){
                for(int c = 1;  c < 56; c++){
                    for(int d = 1; d < 56; d++){
                        for(int e = 1;  e < 56; e++){
                            for(int f = 1; f < 56; f++){
                                if(a != b && a != c && a != c && a != e && a != f && b != c && b != d && b != e && b != f && c != d && c != e && c != f && d != e && d != f && e != f){
                                    word = word2;
                                    numbers = string.Concat(a, separator,  b, separator, c, separator, d, separator, e, separator, f);
                                    word += numbers;
                                    newWords.Add (word);
                                }
                            }
                        }
                    }
                }
            }
        }

        foreach(string line in newWords){
            writeToNewFile.WriteLine(line);
            Console.WriteLine (line);
            wordCount++;
        }

        writeToNewFile.Close ();
        Console.WriteLine (wordCount);
        Console.WriteLine ("Press any key to exit.");
        System.Console.ReadKey ();
    }
}
}

在不尝试修复其余代码的情况下,您需要遵循的基本原则是在获得数字时写出它们,而不是将它们累积在列表中。您可以完全摆脱 ArrayList 以及伴随的 foreach 循环。

相关部分如下所示:

    TextWriter writeToNewFile = new StreamWriter (newFileDestination);

    for(int a = 1; a < 56; a++){
        for(int b = 1; b < 56; b++){
            for(int c = 1;  c < 56; c++){
                for(int d = 1; d < 56; d++){
                    for(int e = 1;  e < 56; e++){
                        for(int f = 1; f < 56; f++){
                            if(a != b && a != c && a != c && a != e && a != f && b != c && b != d && b != e && b != f && c != d && c != e && c != f && d != e && d != f && e != f){
                                word = word2;
                                numbers = string.Concat(a, separator,  b, separator, c, separator, d, separator, e, separator, f);
                                word += numbers;
                                writeToNewFile.WriteLine(word);
                                Console.WriteLine (word);
                                wordCount++;
                            }
                        }
                    }
                }
            }
        }
    }

但是,请注意。您的代码将需要很长时间才能完成。你只是不会得到内存不足的错误。你可能 运行 硬盘用完 space 虽然 :)