Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You get an <code>OutOfMemoryException</code> because inside the loop you allocate a string and store it in an <code>ArrayList</code>. The strings have to stay in memory until the <code>ArrayList</code> is garbage collected and your loop creates more strings than you will be able to store.</p> <p>If you simply want to check the string for a condition you should put the check inside the loop:</p> <pre><code>for ( ... some crazy loop ...) { var word = ... create word ... if (!WordPassesTest(word)) { Console.WriteLine(word + " failed test."); return false; } } return true; </code></pre> <p>Then you only need storage for a single word. Of course, if the loop is crazy enough, it will not terminate before the end of the universe as we know it.</p> <p>If you need to execute many nested but similar loops you can use recursion to simplify the code. Here is an example that is not incredible efficient, but at least it is simple:</p> <pre><code>Char[] chars = "ABCD".ToCharArray(); IEnumerable&lt;String&gt; GenerateStrings(Int32 length) { if (length == 0) { yield return String.Empty; yield break; } var strings = chars.SelectMany(c =&gt; GenerateStrings(length - 1), (c, s) =&gt; c + s); foreach (var str in strings) yield return str; } </code></pre> <p>Calling <code>GenerateStrings(3)</code> will generate all strings of length 3 using lazy evaluation (so no additional storage is required for the strings).</p> <p>Building on top of an <code>IEnumerable</code> generating your strings you can create primites to buffer and process buffers of strings. An easy solution is to using Reactive Extensions for .NET. Here you already have a <code>Buffer</code> primitive:</p> <pre><code> GenerateStrings(3) .ToObservable() .Buffer(10) .Subscribe(list =&gt; ... ship the list to another computer and process it ...); </code></pre> <p>The lambda in <code>Subscribe</code> will be called with a <code>List&lt;String&gt;</code> with at most 10 strings (the parameter provided in the call to <code>Buffer</code>).</p> <p>Unless you have an infinte number of computers you will still have to pull the computers from a pool and only recycle them back to the pool when they have finished the computation.</p> <p>It should be obvious from the comments on this question that you will not be able to process 26^30 strings even if you have multiple computers at your disposal.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload