title: Insufficient system resources exist to complete the requested service when using GeneticSharp
body: <p>Long story short, I'm using GeneticSharp for an iterative/conditional reinforcement learning algorithm. This means that I'm making a bunch of different GeneticAlgorithm instances, each using a shared SmartThreadPool. Only one GA is running at a time though.</p>
  
  <p>After a few iterations of my algorithm, I run into this error, which happens when attempting to start the SmartThreadPool.</p>
  
  <p>Is there any obvious reason this should be happening? I've tried using a different STPE and disposing of it each time, but that didn't seem to help either. Is there some manual cleanup I need to be doing in between each GA run? Should I be using one shared GA instance?</p>
  
  <p>Edit: Quick code sample</p>
  
  <pre><code>static readonly SmartThreadPoolTaskExecutor Executor = new SmartThreadPoolTaskExecutor() { MinThreads = 2, MaxThreads = 8 };
  
  public static void Main(string[] args)
  {
      var achromosome = new AChromosome();
      var bchromosome = new BChromosome();
  
      while(true)
      {
          achromosome = FindBestAChromosome(bchromosome);
          bchromosome = FindBestBChromosome(achromosome);
  
          // Log results;
      }
  }
  
  public static AChromosome FindBestAChromosome(BChromosome chromosome)
  {
      AChromosome result;
  
      var selection = new EliteSelection();
      var crossover = new UniformCrossover();
      var mutation = new UniformMutation(true);
      using (var fitness = new AChromosomeFitness(chromosome))
      {
          var population = new Population(50, 70, chromosome);
          var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
          ga.Termination = new GenerationNumberTermination(100);
          ga.GenerationRan += LogGeneration;
          ga.TaskExecutor = Executor;
          ga.Start();
  
          LogResults();
          result = ga.BestChromosome as AChromosome;
  
          ga.GenerationRan -= LogGeneration;
      }
  
      return result;
  }
  
  public static BChromosome FindBestBChromosome(AChromosome chromosome)
  {
      BChromosome result;
  
      var selection = new EliteSelection();
      var crossover = new UniformCrossover();
      var mutation = new UniformMutation(true);
      using (var fitness = new BChromosomeFitness(chromosome))
      {
          var population = new Population(50, 70, chromosome);
          var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
          ga.Termination = new GenerationNumberTermination(100);
          ga.GenerationRan += LogGeneration;
          ga.TaskExecutor = Executor;
          ga.Start();
  
          LogResults();
          result = ga.BestChromosome as BChromosome;
  
          ga.GenerationRan -= LogGeneration;
      }
  
      return result;
  }
  </code></pre>
  
  <p>AChromosome and BChromosome are each fairly simple, a couple doubles and ints and maybe a function pointer (to a static function).</p>
  
  <p>Edit2: Full call stack with replaced bottom two entries</p>
  
  <pre><code>Unhandled Exception: System.IO.IOException: Insufficient system resources exist to complete the requested service.
  
      at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
      at System.Threading.EventWaitHandle..ctor(Boolean initialState, eventResetMode mode, string name)
      at Amib.Threading.SmartThreadPool..ctor()
      at GeneticSharp.Infrastructure.Threading.SmartThreadPoolTaskExecutor.Start()
      at GeneticSharp.Domain.GeneticAlgorithm.EvaluateFitness()
      at GeneticSharp.Domain.GeneticAlgorithm.EndCurrentGeneration()
      at GeneticSharp.Domain.GeneticAlgorithm.EvolveOneGeneration()
      at GeneticSharp.Domain.GeneticAlgorithm.Resume()
      at GeneticSharp.Domain.GeneticAlgorithm.Start()
      at MyProject.Program.FindBestAChromosome(BChromosome chromosome)
      at MyProject.Program.Main(String[] args)
  </code></pre>
  
  <p>Edit3: One last thing to note is that my fitness functions are pretty processing-intensive and one run can take almost 2g of ram (running on a machine with 16g, so no worries there). I've seen no problems with garbage collection though.</p>
  
  <p>So far, this only happens after about 5 iterations (which takes multiple hours).</p>