Allocation failed - process out of memory

Topics: General
May 31, 2013 at 5:46 PM
Hi,

I am trying to compile my file in node.js and I am getting the error FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory. The file size is rather small.

This is happening on version 0.8.3

Thanks
Developer
May 31, 2013 at 7:47 PM
Can you post some code that exhibits this issue?
May 31, 2013 at 11:36 PM
This is the code
import express = module('express');
var http = require('http');
var a = "http://localhost:1337/ScheduledTasks/88888888/5-22-2013/5-29-2013?authentication=server";

//var objt = JSON.parse(result);
////     console.log(object);
//var dates = [];
//for (var i in objt) {
//    var d = new Date(objt[i].date);
//    dates.push(d);
//}
//ObserveDateArray(dates).subscribe(function (f) { console.log("FIre", f); });

function ObserveUrl(url) {
    var observable = Rx.Observable.create(function (observer) {
        http.get(url, function (res) {
            var result = "";
            res.on('data', function (chunk) {
                result += chunk;
            });
            res.on('end', function () {
                observer.onNext(result);
                observer.onCompleted();
            });
        }).on('error', function (e) {
            observer.onError(e);
        });
        // Return the dispose function 
        return function dispose() {
            // Do nothing
        };
    });
    return observable;
};




var Rx = require('rxjs');

var ObserveDateArrayFromArrayObservable = function (arrayObservable, dateSelector: (any) => Date) {

    var observable = Rx.Observable.create(function (observer) {
        //var subject = new Rx.Subject();
        var currentDisposable = null;
        //subject.subscribe(observer);

        var ourObserver = Rx.Observer.create(
            function onNext(array) {
                var observableForThisArray = ObserveDateArray(array, dateSelector);
                if (currentDisposable != null) {
                    console.log("dei");
                    // No need to listen on old array
                    currentDisposable.dispose();
                    currentDisposable = null;
                }
                currentDisposable = observableForThisArray.subscribe(
                    function (item) {
                        observer.onNext(item);
                    },
                    function (error) {
                        // Ignore errors in array items
                    },
                    function () {
                        // Ignore completion in array items
                    });
            },

            function onError(error) {
                observer.onError(error);
            },
            function onCompleted() {
                // We do not kill the child stream on completion as it will wait for the timers to happen (We will never provide an onCompleted
            }
        );
        var ourObservation = arrayObservable.subscribe(ourObserver);

        // Return the dispose function 
        return function dispose() {
            if (currentDisposable != null) {
                currentDisposable.dispose();
                currentDisposable = null;
            }
            ourObservation.dispose();
        };
    });

    return observable;
};

var ObserveDateArray = function (array: any[], dateSelector: (any) => Date): any {
    var subject = new Rx.Subject();
    var subscribers = 0;
    var timerDisposable = null;
    array = array.sort(function (a, b) { return dateSelector(a).getTime() - dateSelector(b).getTime(); });
    var fireNext = function () {
        console.log(array.length);
        if (array.length > 0) {
            var first = array.shift();
            console.log("waiting for", first);
            if (timerDisposable != null) {
                timerDisposable.dispose();
                timerDisposable = null;
            }
            if (subscribers > 0) {
                timerDisposable = Rx.Observable.timer(dateSelector(first)).subscribe(function (f) {
                    subject.onNext(first);
                    fireNext();
                });
            }
        }
        else {
            subject.onCompleted();
        }
    };

    var fireFirst = function () {
        var now = new Date();
        // Skip times from the past
        while (array.length > 0 && dateSelector(array[0]).getTime() < now.getTime()) {
            array.shift();
        }
        fireNext();
    }
    var observableResult = Rx.Observable.create(function (observable) {
        var subjectDisposable = subject.subscribe(observable);

        subscribers++;
        if (subscribers == 1) {
            // THis is the first subscriber lets invoke the timer
            fireFirst();
        }
        // Should return the dispose function
        return function dispose() {
            subscribers--;
            if (subscribers == 0) {
                if (timerDisposable != null) {
                    timerDisposable.dispose();
                    timerDisposable = null;
                }
            }
            subjectDisposable.dispose();
        };
    });
    return observableResult;
}


var getScheduleFromResult = function (result) {
    var objt = JSON.parse(result);
    var dates = [];
    for (var i in objt) {
        objt[i].date = new Date(objt[i].date);
        dates.push(objt[i]);
    }
    return dates;
};

//var arrayObservable = ObserveUrl("http://localhost:1337/ScheduledTasks/88888888/5-22-2013/5-29-2013?authentication=server").select(function (result) {
//    return getScheduleFromResult(result);
//});

var observeAllScheduledTasks = function (baseUrl, urlSuffix) {
    var observableResult = Rx.Observable.create(function (observer) {
        var currentObserveUrlDisposable = null;
        var now = new Date();
        var midnight = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
        var dayInMiliseconds = 60 * 1000; //60 * 60 * 1000 * 24;
        var timerDisposable = Rx.Observable.timer(midnight, dayInMiliseconds).subscribe(
            function onNext() {
                var now = new Date();
                console.log("Refreshing scheduled tasks", now);
                var todayString = (now.getMonth() + 1) + "-" + now.getDate() + "-" + now.getFullYear();
                now.setDate(now.getDate() + 14);
                var twoWeeksString = (now.getMonth() + 1) + "-" + now.getDate() + "-" + now.getFullYear();
                var url = baseUrl + todayString + "/" + twoWeeksString + urlSuffix;
                console.log(url);
                if (currentObserveUrlDisposable != null) {
                    currentObserveUrlDisposable.dispose();
                    currentObserveUrlDisposable = null;
                }
                currentObserveUrlDisposable = ObserveUrl(url).subscribe(
                        function onNext(response) {
                            observer.onNext(getScheduleFromResult(response));
                        }

                        // We ignore errors and completion as the sub URL will get a constant stream of them
                    );
            },
            function onError(error) {
                console.log("Error from timer", error);
            },
            function onCompleted() {
                console.log("Timer done");
            });
        return function dispose() {
            if (currentObserveUrlDisposable != null) {
                console.log("disposed");
                currentObserveUrlDisposable.dispose();
                currentObserveUrlDisposable = null;
            }
            timerDisposable.dispose();
        };
    });
    return observableResult;
};

var arrayObservable = observeAllScheduledTasks("http://localhost:1337/ScheduledTasks/88888888/", "?authentication=server");

var ob = ObserveDateArrayFromArrayObservable(arrayObservable, function (o) { return o.date });
var fiveMoreMinutes = new Date();
fiveMoreMinutes.setSeconds(fiveMoreMinutes.getSeconds() + 10);

var fiveMoreMinutes2 = new Date(fiveMoreMinutes.getTime());
fiveMoreMinutes2.setSeconds(fiveMoreMinutes.getSeconds() + 10);

var f = new Date(fiveMoreMinutes.getTime());
f.setSeconds(fiveMoreMinutes.getSeconds() - 30);
//
//d.dispose();
var disposable = null;
disposable = ob.subscribe(function (f) { console.log(f); });
//var timerDisposable = Rx.Observable.timer(fiveMoreMinutes).subscribe(function () { disposable = ob.subscribe(function (f) { console.log(f); }); } );
var timerEnd = null;
timerEnd = Rx.Observable.timer(fiveMoreMinutes2).subscribe(function (f) { console.log("disposing"); disposable.dispose(); timerEnd.dispose() });


//var a = Rx.Observable.timer(fiveMoreMinutes).select(function (f) { return "Yair"; });
//a.subscribe(function (f) { console.log("A", f); });
//var b = Rx.Observable.timer(fiveMoreMinutes).select(function (f) { return "Moriya"; });
//b.subscribe(function (f) { console.log("b", f); });
//a.merge(b).subscribe(function (f) { console.log("Merged", f); });


//d.dispose();
May 31, 2013 at 11:47 PM

Note that this code is my playground so ignore coding style

Jun 2, 2013 at 6:05 AM
I made a deep dive and I think I know what the issue is.

When I add names to anonymous functions I get this failure. So in the code above the failure came from
        var timerDisposable = Rx.Observable.timer(midnight, dayInMiliseconds).subscribe(
            function ____onNext____() {
                var now = new Date();
                console.log("Refreshing scheduled tasks", now);
                var todayString = (now.getMonth() + 1) + "-" + now.getDate() + "-" + now.getFullYear();
                now.setDate(now.getDate() + 14);
                var twoWeeksString = (now.getMonth() + 1) + "-" + now.getDate() + "-" + now.getFullYear();
                var url = baseUrl + todayString + "/" + twoWeeksString + urlSuffix;
                console.log(url);
                if (currentObserveUrlDisposable != null) {
                    currentObserveUrlDisposable.dispose();
                    currentObserveUrlDisposable = null;
                }
                currentObserveUrlDisposable = ObserveUrl(url).subscribe(
                        function onNext(response) {
                            observer.onNext(getScheduleFromResult(response));
                        }

                        // We ignore errors and completion as the sub URL will get a constant stream of them
                    );
            },
            function ____onError____(error) {
                console.log("Error from timer", error);
            },
            function ____onCompleted____() {
                console.log("Timer done");
            });
Oct 3, 2013 at 9:56 PM
I have the same kind of error. I can't anymore compile my library (https://github.com/srenault/immutable.ts).
For info, I use the last commit this day (0bbee3396b1e082a45951204900c7079fb11901d) of the develop branch.
Is there someone help me ?
Oct 4, 2013 at 9:18 AM
Have your tried 0.9.1.1, ?

I had a project I started with 0.8.3 and it worked fine with 0.9.1.1, I think support for 0.8 might be limited now that 0.9 has been out for a while.

The main change I think with 0.9 was the change of bool to boolean, but the compiler does have the --allowbool option to make compiling 0.8 code easer.
Oct 4, 2013 at 4:14 PM
Sorry but I don't use the 0.8 version of Typescript but the current branch 'develop' of the typescript repository. So, I have the last changes. However, I'm facing up the same issue above: "Allocation failed - process out of memory"
Coordinator
Oct 4, 2013 at 10:32 PM
Is this still an issue? If so, would you mind filing an issue in the issue tracker so we can look into it further?
Oct 5, 2013 at 11:02 AM
Ok I will fill an issue.