There is a script:

var timer = 0; function save_price() { var fs = require("fs"), request = require("request"), config = require('./server.config.js').config, IGetMarketPrices_URL = config.IGetMarketPrices_URL, myData = {"response":{"success":0,"message":"You can only request this page every 300 seconds per API key. Try again in 176 seconds."}}; timer = setInterval(function(){ request({ url: IGetMarketPrices_URL, json: true }, function (error, response, body) { if (!error && response.statusCode === 200) { myData = body; fs.writeFile('../var/www/prices.json', JSON.stringify(myData, null, 4), function(err) { if(err) console.log(err); else console.log("JSON saved to " + '../var/www/prices.json'); }); fs.writeFile('prices.json', JSON.stringify(myData, null, 4), function(err) { if(err) console.log(err); else console.log("JSON saved to " + 'prices.json'); }); } }); }, 10800000);//10800000 = каждые 3 часов //clearInterval(timer); //save_price(); } save_price(); 

Attention, the question is: why does the script eat 105 mb in 8 hours of uptime, although there is only one cycle running every 3 hours, is it possible to modernize it somehow?

Если код кривой, буду рад увидеть поправки, спасибо.

Here is the screen: Screen demon on the server

    1 answer 1

    node.js takes the external memory until it reaches the maximum allowed heap size (approximately 1.5 GB if memory does not change), or until the system starts to require memory back (stop giving).

    To check if a memory leak occurs, you can add the following piece:

     setInterval(function(){ //из Вашего кода global.gc(); console.log('Memory usage:', process.memoryUsage()); //остальной код 

    Run node.js with the --expose-gc flag, look at the output ( heapUsed value).

    If the heapUsed value fluctuates within certain limits, then there is no leakage, and 105 MB of node.js are “eaten off”, just to make garbage collection less often. But judging by this code, there should be no leaks, so you need not worry.

    As part of the code:

    • at a minimum, declarations and require variables are best left outside of the function.
    • JSON.stringify() easier to save in a variable, so as not to do it 2 times
    • I don’t quite understand why the variable myData , given that it is initiated 1 time and is not used anywhere (the assignment of body in myData , followed by JSON.stringify() does not make sense in this case)
    • maybe it makes sense to add logging of query execution error
    • just need the first call of the request delayed by the interval value?
    • Thank you very much, the memory ranges from 100 to 127, no longer eats, then there is no leakage. But I have 2 servers with the same code that is there, that is there, and here: on one script, 29 MB of eats, and on the second one 127, what could be the problem? For the advice, thank you very much, I took advantage of them. - Vladislav Siroshtan
    • If these memory measures are heapUsed , then the only thing I can assume is different versions of node.js (with absolutely identical scripts). But this is something too strange. If this is the size of the memory occupied by the process, then most likely the first is just less free memory, so node.js shrinks the heap. - NumminorihSF
    • Apparently it is, on the server where it eats 29 there 1GB of RAM, and where 100-127 is 2GB of RAM. - Vladislav Siroshtan