简体   繁体   English

当没有什么东西应该那么大时,使用 nodejs 处理内存不足错误

[英]Process out of memory error using nodejs when nothing should be that large

The exact error says "FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory" The program should get the prime number in the nth position but fails no matter how small the number is.确切的错误是“致命错误:CALL_AND_RETRY_LAST 分配失败 - 进程内存不足”该程序应该在第 n 个位置获取质数,但无论数字多小都会失败。 What exactly does this error mean and how do I fix it?这个错误究竟是什么意思,我该如何解决?

var primes = [2, 3, 5];
var x = 0;
var y = 0;
var z = 6;
var nthPrime = function(number){
    while (primes.length <= number){
        y = 0;
        while (y <= primes.length){
            x = primes[y];
            if (z % x === 0){
                y++;
            } else{
                primes.push(z);
                z++;
            }
        }
    }
    console.log(primes[number]);
};
nthPrime(3);

The second while loop is an infinite loop.第二个while循环是一个无限循环。 The else statement is happening more frequently than the if statement, after the first two iterations.在前两次迭代之后, else语句比if语句更频繁地发生。 This means the primes array is increasing in length faster than the y value.这意味着primes数组的长度增加速度快于y值。 Work out the logic some more and this particular error should work itself out.再计算一下逻辑,这个特定的错误应该会自行解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM