title: Deleting large Javascript objects when process is running out of memory
body: <p>I'm a novice to this kind of javascript, so I'll give a brief explanation:</p>
  
  <p>I have a web scraper built in <code>Nodejs</code> that gathers (quite a bit of) data, processes it with <code>Cheerio</code> (basically <code>jQuery</code> for <code>Node</code>) creates an object then uploads it to mongoDB.</p>
  
  <p>It works just fine, except for on larger sites. What's <em>appears</em> to be happening is:</p>
  
  <ol>
  <li>I give the scraper an online store's URL to scrape</li>
  <li>Node goes to that URL and retrieves anywhere from 5,000 - 40,000 product urls to scrape</li>
  <li>For each of these new URLs, Node's <code>request</code> module gets the page source then loads up the data to <code>Cheerio</code>. </li>
  <li>Using Cheerio I create a JS object which represents the product. </li>
  <li>I ship the object off to MongoDB where it's saved to my database.</li>
  </ol>
  
  <p>As I say, this happens for thousands of URLs and once I get to, say, 10,000 urls loaded I get errors in node. The most common is:</p>
  
  <pre><code>Node: Fatal JS Error: Process out of memory
  </code></pre>
  
  <p>Ok, here's the actual question(s):</p>
  
  <p>I <em>think</em> this is happening because Node's garbage cleanup isn't working properly. It's possible that, for example, the <code>request</code> data scraped from all 40,000 urls is still in memory, or at the very least the 40,000 created javascript objects may be. Perhaps it's also because the MongoDB connection is made at the start of the session and is never closed (I just close the script manually once all the products are done). This is to avoid opening/closing the connection it every single time I log a new product.</p>
  
  <p>To really ensure they're cleaned up properly (once the product goes to MongoDB I don't use it anymore and can be deleted from memory) can/should I just simply delete it from memory, simply using <code>delete product</code>?</p>
  
  <p>Moreso (I'm clearly not across how JS handles objects) if I delete one reference to the object is it totally wiped from memory, or do I have to delete all of them?</p>
  
  <p>For instance:</p>
  
  <pre><code>var saveToDB = require ('./mongoDBFunction.js');
  
  function getData(link){
      request(link, function(data){
          var $ = cheerio.load(data);
          createProduct($)
      })
  }
  
  function createProduct($)   
      var product = {
          a: 'asadf',
          b: 'asdfsd'
          // there's about 50 lines of data in here in the real products but this is for brevity
      }    
      product.name = $('.selector').dostuffwithitinjquery('etc');
      saveToDB(product);
  }
  
  // In mongoDBFunction.js
  
  exports.saveToDB(item){
      db.products.save(item, function(err){
          console.log("Item was successfully saved!");
          delete item; // Will this completely delete the item from memory?
      })
  }
  </code></pre>