Answer the question
In order to leave comments, you need to log in
How to organize an infinite loop (or infinite recursion) in Node.JS without a memory leak?
Good afternoon!
The task is to develop a JavaScript program (in principle) with an unlimited work cycle using Node.JS. There was a problem that the program eats up memory. The best solution was the following code:
loop();
function loop() {
//Тут произвольный код
setTimeout(function() {
loop();
}, 8);
}
for(;;){
//Тут произвольный код
usleep(8000);
}
Answer the question
In order to leave comments, you need to log in
Lynn's "Coffeeman" solution
( see comments under question) helped optimize the program! For the first 2-3 hours, the situation was worse than with my solution (I assume that the code has become simpler and the number of iterations per unit of time has increased, thus the speed of eating memory has also increased) , but for reasons I do not understand, after 6-8 hours the memory began to return to the system, and even in what volume (memory consumption decreased from 6.6mb to 3.4mb) . What happened is not clear. The script is working normally. Yes, it still eats up memory, but after a few hours it abruptly gives half to the system. There is something to think about.
For those interested - OS: Ubuntu 18.04.1; platform - Node.JS 8.10; program code:
loop();
function loop() {
setTimeout(loop, 8);
}
Infinite Loop in JS:
while (true) {
// do something here
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question