Skip to content

Commit

Permalink
SERVER-20770 garbage collect every 10 MB in itcount() instead of ever…
Browse files Browse the repository at this point in the history
…y 10000 documents

Ensures that the shell will not consume too much memory while using
DBCommandCursor to iterate a large result set.
  • Loading branch information
dstorch committed Oct 6, 2015
1 parent 0eb5deb commit a7aa0d4
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/mongo/shell/query.js
Expand Up @@ -367,16 +367,22 @@ DBQuery.prototype.countReturn = function(){
*/
DBQuery.prototype.itcount = function(){
var num = 0;

// Track how many bytes we've used this cursor to iterate iterated. This function can be called
// with some very large cursors. SpiderMonkey appears happy to allow these objects to
// accumulate, so regular gc() avoids an overly large memory footprint.
//
// TODO: migrate this function into c++
var bytesSinceGC = 0;

while ( this.hasNext() ){
num++;
this.next();

// This function can be called with some very large cursors.
// SpiderMonkey appears happy to allow these objects to accumulate, so
// regular gc() avoids an overly large memory footprint.
//
// TODO: migrate this function into c++
if (num % 10000 == 0) {
var nextDoc = this.next();
bytesSinceGC += Object.bsonsize(nextDoc);

// Garbage collect every 10 MB.
if (bytesSinceGC > (10 * 1024 * 1024)) {
bytesSinceGC = 0;
gc();
}
}
Expand Down

0 comments on commit a7aa0d4

Please sign in to comment.