-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strange memory leaks #3171
Comments
UPDATE I can see now that this is all about the console output. If we write into the console fast, Node JS process quickly consumes over 1GB of memory and then crashes. I did not expect that. Turning off the console output stops the memory leak. Is this a known problem and/or how to deal with such a problem? |
Hi I played around with your sample in my environment. A few comments. node.js single threaded, you are looping it , this is not the "recommended" way to use node. The process will get into 100% CPU.. I trigger a core dump and checked the memory and found that it has a large number functions and object type being allocated. !jsobject 0x00001004b2f18b29 JS_FUNCTION_TYPE 8411788 5 length, name, arguments, caller, prototype |
This is a known "issue" since writing to stdout in the case of a tty/console is async. So logging a lot of data very fast could very well cause a lot of writes to be buffered in memory if the tty/console cannot keep up. Try writing to a file (synchronously) instead (e.g. |
Try redirecting the output to a file, it should help as a work-around. |
If this is literally all your script is doing, use function nextPrime(value) {
if (value > 2) {
var i, q;
do {
i = 3;
value += 2;
q = Math.floor(Math.sqrt(value));
while (i <= q && value % i) {
i += 2;
}
} while (i <= q);
return value;
}
return value === 2 ? 3 : 2;
}
var i = 0;
var value;
function doNextPrime() {
if (i < 1000000000) {
value = nextPrime(value);
console.log(value);
i++;
setImmediate(doNextPrime);
}
}
doNextPrime(); Node isn't built to do extensive primitive loops like what you're doing. In my opinion this is less Node's fault and more the misunderstanding of how Node's IO works. What Running the modified program doesn't cause it to slow down as much and memory hovers around ~45M. |
@Qix- The example you provided does not work. |
@ChALkeR it does; had a typo in there. Fixed. |
@Qix- Ok, it works now after the fix. Note that your answer is not correct, though. Using Example: var str = 'dssf'.repeat(100);
function next() {
for (var i = 0; i < 100; i++) {
console.log(str);
}
setImmediate(next);
}
next(); |
@ChALkeR take a closer look at the example. The call to |
Edit Ah, sorry. It's not in the loop, I missed that. |
@Qix- Still it fixes things here only by making it piping slower, not by the means you described. See the example in #3171 (comment). |
@ChALkeR What do you mean making piping slower? |
@vitaly-t I have the same problem ,can you solved it?
|
@zhoujinhai |
@vitaly-t thanks |
logging to stdout very fast causes node to crash with heap out of memory nodejs/node#3171
I'm using NodeJS 4.1.1, 64-bit on Windows 10.
In my simple application I'm calculating a sequence of primes and writing them into the console.
Here's the entire application:
However, when running it, the application starts eating up memory quite fast. I don't understand why, since I am making no memory allocation of any kind, it is just a plain calculation algorithm without any complexity.
After running it for about 5 mins, the app crashes as shown below, before it even gets to calculate the first 100m primes.
The text was updated successfully, but these errors were encountered: