Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to improve this code with memory leak in nodejs with zlib?
    text
    copied!<p>I have problem with memory leak in nodejs when I added gzip support. I have wrote some code for tests. In code below I use zlib and I don't know why I have memory leak. How I can improve this code to avoid memory leaking ? Anybody can help me ?</p> <pre><code>var zlib = require('zlib'), crypto = require('crypto'); var cacheList = { article: {}, }; var timeoutId1, timeoutId2 console.log('process.pid: '+ process.pid); clean = function() { var time = new Date().getTime(); timeoutId1 = setTimeout(function() { clean() }, 5000); var countDeleted = 0; for (id in cacheList.article) { if (cacheList.article[id] &amp;&amp; cacheList.article[id].timeExpire + 5000 &lt; time) { delete cacheList.article[id]; countDeleted++; } } console.log('deleted: ' + countDeleted); </code></pre> <p>}</p> <pre><code> run = function() { var time = new Date().getTime(); timeoutId1 = setTimeout(function() { run() }, 5); var md5 = crypto.createHash('md5'); md5.update('' + time); var id = md5.digest('hex'); //console.log('id: ' + id); var text = id+id+id+id+id+id+id+id +id+id+id+id+id+id+id+id +id+id+id+id+id+id+id+id +id+id+id+id+id+id+id+id +id+id+id+id+id+id+id+id +id+id+id+id+id+id+id+id +id+id+id+id+id+id+id+id +id+id+id+id+id+id+id+id; zlib.gzip(text, function(err, result) { if (!err) { cacheList.article[id] = { timeExpire: time + 10000, data: text, datagzip: result, }; } }); </code></pre> <p>}</p> <pre><code> timeoutId1 = setTimeout(function() { run() }, 3000); timeoutId2 = setTimeout(function() { clean() }, 5000); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload