Skip to content
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

Eats all memory very quickly then killed by OOM #39

Open
ZiCog opened this issue Feb 9, 2015 · 2 comments
Open

Eats all memory very quickly then killed by OOM #39

ZiCog opened this issue Feb 9, 2015 · 2 comments

Comments

@ZiCog
Copy link

ZiCog commented Feb 9, 2015

node-bignum uses all the memory in my system.

I have, as a test of various big number modules, been calculating the 4784969th number in the Finonacci sequence. This is the first Fibonacci number of one million decimal digits. See code below. Whilst node-bignum seems to be a lot faster than say big-integer by a factor of about 20 for fibo(40000) it does use a lot of memory. fibo(400000) uses half of my 4GB RAM already! For comparison big-integer only uses 3% of my memory when calculating fibo(4784969).

I am doing something wrong, is this normal or a bug?

Edit: Using node 0.11.14 here.
Edit: Same problem with 0.10.32

var bigNumber = require("bignum");  

function fibo_iterative(n) {
    var first = bigNumber(0),
        second = bigNumber(1),
        next = bigNumber(0),
        c;
    if (n <= 1) {
        return bigNumber(n);
    }
    for (c = 0; c < n - 1; c += 1) {
        next = first.add(second);
        first = second;
        second = next;
    }
    return next;
}

var resultString;
var n = 4784969;  // The first fibo number with one million digits!
resultString = fibo_iterative(n).toString();
console.log("The " + resultString.length + " digits of fibo(" + n + ") are:");
console.log(resultString);
console.log("Done.");
@rvagg
Copy link
Collaborator

rvagg commented Feb 18, 2015

Unfortunately I don't have a good answer for you here ; it could be a GC problem with the large loop, perhaps if it was broken up across event loop turns you might have different results, unfortunately that's not a great answer because it would significantly complicate your code.

I'll leave this issue open in the hope that someone else can answer it--I'm more of a maintainer rather than an expert user unfortunately!

@ZiCog
Copy link
Author

ZiCog commented Feb 18, 2015

I tried your suggestion and iterated the loop via a setInterval(), also tried a 1ms setTimeout().

This reduces my CPU load down a few percent and I guess it would take a little over an hour to complete.

Sadly it still ramps up to 30% of my 3GB RAM here after a few minutes.

Here is the code:
var bigNumber = require("bignum");

function fibo_iterative(n, callback) {
    var first = bigNumber(0),
        second = bigNumber(1),
        next = bigNumber(0),
        loop,
        c;
    if (n <= 1) {
         callback (bigNumber(n));
    } else {
        c = 0;
        (function loop () {
            if (c < n - 1) {
                next = first.add(second);
                first = second;
                second = next;
                c += 1;
                //setImmediate(loop);
                setTimeout(loop, 1);
            } else {
                callback (next);
            }
        }());
    }
}
var n = 4784969;  // The first fibo number with one million digits!
fibo_iterative(n, function (r) {
    var rs = r.toString();
    console.log("The " + rs.length + " digits of fibo(" + n + ") are:");
    console.log(rs);
    console.log("Done.");
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants