title: Node.js: How to release Mongoose model from memory? (memory leak)
body: <p>How to release the model from memory after accessing it? I'm using <code>global.gc()</code> with <code>--expose-gc</code> option to see that this is eating my memory.</p>
  
  <p>The following code creates 10000 collections:</p>
  
  <pre><code>// mongoose connection
  var db = mongoose.createConnection(...);
  
  // amount of collections
  var amount = 10000;
  
  // create collections (100/per second)
  var sync = async.queue(function(n, cb) {
      var schema = new mongoose.Schema({
          data: mongoose.Schema.Types.Mixed
      });
      var collection = 'model_'+n;
      var model = db.model(collection, schema);
      setTimeout(function() { cb(); }, 10);
  }, 1);
  
  // push to queue
  for(var i=0; i&lt;amount; i++) {
      sync.push(i);
  }
  
  // done
  sync.drain = function(err) {
      console.log('all '+amount+' models done');
  };
  
  // garbage collector (every second)
  setInterval(function() {
      try { global.gc(); } catch(gcerr) { }
  }, 1000);
  </code></pre>
  
  <p>The memory usage is increasing as collections are created and the memory is never released:</p>
  
  <pre><code>1 - Memory used: 30 MB
  2 - Memory used: 36 MB
  3 - Memory used: 42 MB
  4 - Memory used: 48 MB
  5 - Memory used: 54 MB
  6 - Memory used: 61 MB
  7 - Memory used: 65 MB
  8 - Memory used: 71 MB
  9 - Memory used: 77 MB
  10 - Memory used: 82 MB
  all 10000 models done
  11 - Memory used: 86 MB
  12 - Memory used: 86 MB
  </code></pre>
  
  <p>Any ideas how to purge the model from the memory manually without closing the connection?</p>
  
  <p><strong>FOUND SOLUTION</strong></p>
  
  <pre><code>delete db.models[collection];
  delete db.collections[collection];
  delete db.base.modelSchemas[collection];
  </code></pre>