title: CasperJS: Proper way to close page instances while looping
body: <p>I have a long-running script that loops across several different links to perform its work. I encountered a scenario where PhantomJS ran out of resources due to the page objects not being garbage collected since I'm reusing it. I have a simple example below. I close() the page object and create a new one at the end of each loop cycle, but after I do that Casper just hangs. What is the proper way to do this?</p>
  
  <pre><code>var links = ['http://www.google.com', 'http://www.yahoo.com'];
  
  var casper = require('casper').create({
      logging: 'error',
      pageSettings: {
          webSecurityEnabled: false
      }
  });
  
  casper.start('http://www.amazon.com', function () {
      this.echo(this.getTitle());
  });
  
  casper.eachThen(links, function(item) {
      var url = item.data;
      this.open(url).then(function (){
          this.echo(this.getTitle());
      }).then(function (){
          casper.page.close();
          casper.page = require('webpage').create();
      });
  });
  
  casper.run(function (){
      this.echo('Done');
  });
  </code></pre>
  
  <p>This will output</p>
  
  <pre><code>Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs &amp; more
  Google
  </code></pre>
  
  <p>and then just hang.  What am I missing?</p>