From cb23cf2d76514d4b016396739573b033a0a5fedd Mon Sep 17 00:00:00 2001 From: Thaddee Tyl Date: Mon, 21 Sep 2015 14:21:44 +0200 Subject: [PATCH] Reduce cache size Hopefully, this will help determine if the cache is the cause of the memory growth we see. --- lru-cache.js | 7 +++++-- server.js | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lru-cache.js b/lru-cache.js index 7e08f65..c03c2cb 100644 --- a/lru-cache.js +++ b/lru-cache.js @@ -11,6 +11,7 @@ function Cache(size, type) { type = type || 'unit'; this.size = size; this.type = typeEnum[type]; + if (this.type === typeEnum.unit) { this.size -= 1; } // `cache` contains {content, index}. // - content: the actual data that is cached. // - index: the position in `order` of the data. @@ -30,6 +31,7 @@ Cache.prototype = { // If the cache is full, remove the oldest data // (ie, the data requested longest ago.) var numberToRemove = this.limitReached(); + if (numberToRemove > this.order.length) { numberToRemove = this.order.length; } for (var i = 0; i < numberToRemove; i++) { // Remove `order`'s oldest element, the first. delete this.cache[this.order[0]]; @@ -39,7 +41,7 @@ Cache.prototype = { this.cache[cacheIndex] = { index: this.order.length, content: cached, - } + }; this.order.push(cacheIndex); } }, @@ -58,9 +60,10 @@ Cache.prototype = { return this.cache[cacheIndex] !== undefined; }, - // Returns true if we're past the limit. + // Returns the number of elements to remove if we're past the limit. limitReached: function heuristic() { if (this.type === typeEnum.unit) { + // Remove the excess. return Math.max(0, (this.order.length - this.size)); } else if (this.type === typeEnum.heap) { if (getHeapSize() >= this.size) { diff --git a/server.js b/server.js index 7ebab7c..aecb5f0 100644 --- a/server.js +++ b/server.js @@ -157,8 +157,8 @@ var minAccuracy = 0.75; // = 1 - max(1, df) / rf var freqRatioMax = 1 - minAccuracy; -// Request cache size of 500MB (~1000 bytes/image). -var requestCache = new LruCache(500000); +// Request cache size of 50MB (~5000 bytes/image). +var requestCache = new LruCache(10000); // Deep error handling for vendor hooks. var vendorDomain = domain.create();