URKA
URKA

Reputation: 13

NodeJS - FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed

I am having this problem... I am using Windows 7 and Chrome.

I have tried this solution: FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory

But didn't work.

Also tried this other: Devextreme : FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

But I cannot find that file, like if it doesn't exist.

  1. I am trying to do this tutorial.

  2. The code I am executing is this:

var http = require("http"),
fs = require("fs");        

http.createServer(function(req, res){

    fs.readFile("./index_ASYNC.html", function(err, html){    
        var i=0;

        while(true) {    
            i++;    
            res.write(i+""); // Envía respuestas al navegador.
        }

        // res.writeHead(200,{"Content-Type":"text/html"});
        res.end();
    });

}).listen(8080);
  1. I execute it using node hola_html.js.

  2. The resulting error:

<--- Last few GCs --->

[5344:00000000002C05B0] 46772 ms: Mark-sweep 1399.5 (1427.9) -> 1399.5 (1427. 9) MB, 2231.9 / 0.0 ms allocation failure GC in old space requested [5344:00000000002C05B0] 49806 ms: Mark-sweep 1399.5 (1427.9) -> 1399.5 (1426. 9) MB, 2583.4 / 0.0 ms last resort GC in old space requested [5344:00000000002C05B0] 52394 ms: Mark-sweep 1399.5 (1426.9) -> 1399.5 (1426. 9) MB, 2588.3 / 0.0 ms last resort GC in old space requested

<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0000028BF5325EE1 1: _send [_http_outgoing.js:~216] [pc=0000002FDD52590C](this=000000B79B184D2 1 ,data=000003BB18D85CB1 ,encoding=0000010C6FF02201 ,callback=0000010C6FF02201 < null>) 2: /* anonymous */ [C:\Leo\Prodigios\CursoNodeJS\4-encabezados\hola_html_ASY NC_v2.js:~10] [pc=0000002FDD5131F2](this=00000191E280BE21

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memo ry

  1. I must add, I tried the second option I found on Internet (Devextreme : FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory) but it didn't work.

Upvotes: 1

Views: 2382

Answers (1)

sdooo
sdooo

Reputation: 1881

Obviously this example will cause memory leak, this line is causing it:

while(true) {    
   i++;    
   res.write(i+""); // Envía respuestas al navegador.
}

You are looping here infinitely incrementing i and writing to output multiple times (you should only write once).

There is no time for garbage collector to pick this up, and your RAM soon reaches its limit

I don't know language in this video but I believe author was warning about this approach.

Example application could look like this:

var http = require('http'); // import module
http.createServer(function (req, res) { // create http server
  // create header to let browser know what content you are trying to send
  res.writeHead(200, {'Content-Type': 'text/plain'});
  // write string to client
  res.write('Hello World!');
  // end the request
  res.end();
}).listen(8080); // listen to requests on http://localhost:8080

I see in your tutorial there are more complex examples that will work, let's say at 4:25

Upvotes: 0

Related Questions