D
D
Dmitry Belyaev2015-11-16 19:16:08
Node.js
Dmitry Belyaev, 2015-11-16 19:16:08

Memory leak in node.js core?

node.js version 4.1.2
test code:

"use strict";
setInterval(() => {
  console.log(process.memoryUsage());
}, 3000);

I see the following picture:
{ rss: 15212544, heapTotal: 9275392, heapUsed: 3992184 }
{ rss: 15749120, heapTotal: 9275392, heapUsed: 4384144 }
{ rss: 15749120, heapTotal: 9275392, heapUsed: 4411704 }
{ rss: 15749120, heapTotal: 9275392, heapUsed: 4419048 }
{ rss: 15749120, heapTotal: 9275392, heapUsed: 4426152 }
{ rss: 15749120, heapTotal: 9275392, heapUsed: 4433256 }
{ rss: 15749120, heapTotal: 9275392, heapUsed: 4440360 }
{ rss: 15749120, heapTotal: 9275392, heapUsed: 4447464 }
{ rss: 15749120, heapTotal: 9275392, heapUsed: 4454496 }
{ rss: 15749120, heapTotal: 9275392, heapUsed: 4461528 }
{ rss: 15769600, heapTotal: 9275392, heapUsed: 4471152 }
{ rss: 15769600, heapTotal: 9275392, heapUsed: 4478216 }
{ rss: 15769600, heapTotal: 9275392, heapUsed: 4485248 }
{ rss: 15769600, heapTotal: 9275392, heapUsed: 4492280 }
{ rss: 15769600, heapTotal: 9275392, heapUsed: 4499312 }
{ rss: 15781888, heapTotal: 9275392, heapUsed: 4513496 }
{ rss: 15781888, heapTotal: 9275392, heapUsed: 4520560 }
{ rss: 14970880, heapTotal: 9275392, heapUsed: 4539096 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4593392 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4605416 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4614056 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4621088 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4630616 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4637648 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4646240 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4653272 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4663456 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4675008 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4682040 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4689072 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4696104 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4703136 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4710168 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4719760 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4728136 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4735168 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4742200 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4749232 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4756264 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4763296 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4770328 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4777360 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4784392 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4792016 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4813208 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4820144 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4827080 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4834016 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4840952 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4847888 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4858704 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4865640 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4872576 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4879512 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4886448 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4893384 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4900320 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4907256 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4914192 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4921128 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4932416 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4939352 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4946288 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4953224 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4960160 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4967096 }
{ rss: 15036416, heapTotal: 9275392, heapUsed: 4994160 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5021896 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5028864 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5035800 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5042736 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5049672 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5056608 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5063544 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5070480 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5077416 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5084352 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5091288 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5098224 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5120600 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5127472 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5134344 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5141216 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5148088 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5154960 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5161832 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5168704 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5175576 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5182448 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5189320 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5196192 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5203064 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5209936 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5216808 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5223680 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5230552 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5237424 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5244296 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5251168 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5258040 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5264912 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5271784 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5278656 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5285528 }
{ rss: 15446016, heapTotal: 9275392, heapUsed: 5292400 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5299272 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5306176 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5313048 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5319920 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5326792 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5333664 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5340752 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5348920 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5359888 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5366760 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5373632 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5380504 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5387376 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5394248 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5401120 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5407992 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5414864 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5421736 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5428608 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5435480 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5442352 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5449224 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5456096 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5462968 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5469840 }
{ rss: 15712256, heapTotal: 9275392, heapUsed: 5476712 }
{ rss: 15904768, heapTotal: 10295296, heapUsed: 5509192 }

launch is standard:
node memtest.js
is easy to see that memory consumption is constantly increasing, by about 7-9 kb every 3 seconds, i.e. about 220 mb per day, from here you can make that even such a script will fall off approximately at the beginning of the third day of work

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yaroslav Lyzlov, 2015-11-16
@dixoNich

Some kind of synthetic test.
stackoverflow.com/questions/14034107/does-javascri...
javascript.info/tutorial/memory-leaks#setinterval-...
You can run your application with the --expose-gc flag and have fun with gc cleanup yourself.

"use strict";
console.log('start')
var i = 0;
var timeout = null
function fn () {
  i++
  console.log(process.memoryUsage());
  if (i == 10000) { 
    clearTimeout(timeout);
    console.log(process.memoryUsage());
  }
  global.gc();  
}
timeout = setInterval(fn, 10);

N
Nicholas, 2015-11-17
@ACCNCC

Use --expose-gc once per second

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question