From 8d14668992f156d8ec9e5fa31ddd83987db081ea Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 30 Oct 2012 01:19:01 +0100 Subject: zlib: reduce memory consumption, release early In zlibBuffer(), don't wait for the garbage collector to reclaim the zlib memory but release it manually. Reduces memory consumption by a factor of 10 or more with some workloads. Test case: function f() { require('zlib').deflate('xxx', g); } function g() { setTimeout(f, 5); } f(); Observe RSS memory usage with and without this commit. After 10,000 iterations, RSS stabilizes at ~35 MB with this commit. Without, RSS is over 300 MB and keeps growing. Cause: whenever the JS object heap hits the high-water mark, the V8 GC sweeps it clean, then tries to grow it in order to avoid more sweeps in the near future. Rule of thumb: the bigger the JS heap, the lazier the GC can be. A side effect of a bigger heap is that objects now live longer. This is harmless in general but it affects zlib context objects because those are tied to large buffers that live outside the JS heap, on the order of 16K per context object. Ergo, don't wait for the GC to reclaim the memory - it may take a long time. Fixes #4172. --- lib/zlib.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'lib/zlib.js') diff --git a/lib/zlib.js b/lib/zlib.js index 7837f3f7b..b0826a8f5 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -150,7 +150,10 @@ function zlibBuffer(engine, buffer, callback) { } function onEnd() { - callback(null, Buffer.concat(buffers, nread)); + var buf = Buffer.concat(buffers, nread); + buffers = []; + callback(null, buf); + engine._clear(); } engine.on('error', onError); @@ -353,6 +356,10 @@ Zlib.prototype.end = function end(chunk, cb) { return ret; }; +Zlib.prototype._clear = function() { + return this._binding.clear(); +}; + Zlib.prototype._process = function() { if (this._hadError) return; -- cgit v1.2.1