0

Getting an out of memory of error when no memory storage is used except two variables(i & j). Can someone please clarify.

I'm not storing anywhere in memory or writing it to any storage, whatever is generated gets spits to console and need to move to next instruction.

console.log('num,num2,op,result');
for(i=9.0;i>=0;) {
  for(j=9.0;j>=0;) {
     console.log(i.toFixed(3) + ',' + j.toFixed(3) + ',+,'  + (i+j).toFixed(3));
     j =parseFloat((j - 0.001).toFixed(3));
  }
     i =parseFloat((i - 0.001).toFixed(3));
 }

Executing this getting stack error,

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

 1: node::Abort() [/usr/local/bin/node]
 2: node::FatalException(v8::Isolate*, v8::Local<v8::Value>, v8::Local<v8::Message>) [/usr/local/bin/node]
 3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/local/bin/node]
 4: v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [/usr/local/bin/node]
 5: v8::internal::Runtime_AllocateInTargetSpace(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/bin/node]
 6: 0x3006473079a7
Abort trap: 6

Please let me know what I'm missing to avoid memory crash.

More Research:

To our code, it does not make any difference whether console.log is async or not, it does not provide any kind of callback or so; and the values you pass are always referenced and computed at the time you call the function.

Reference: console.log() async or sync?

It references the data only at the time of referring the data associated with it.

Will also check for any memory leaks associated with parseFloat or toFixed.

Kannaiyan
  • 12,554
  • 3
  • 44
  • 83
  • *memory* means RAM here, and every variable etc. resides in the RAM. But no, your code does not use much memory so the memory leak must be somewhere else – Jonas Wilms Oct 22 '17 at 18:01
  • The only variable used is i & j. I'm not seeing anything holding on to RAM. – Kannaiyan Oct 22 '17 at 18:04
  • 1
    @Kannaiyan `console.log` will use a lot of ram, also the `toFixed` and `parseFloat` functions will use ram - just like every other operation that doesn't fit into registers. – Bergi Oct 22 '17 at 18:05
  • But once console.log completes, it goes to console and ram will be cleared right. Is there anyway I can flush it out of process? I under toFixed and parseFloat are functions, they should also clear their memory once function is complete right? Also assuming they are synchronous. I'm not seeing that will run their asynchronously. – Kannaiyan Oct 22 '17 at 18:10
  • Yes, the only thing that continues to acquire memory is the console output buffer, the rest should remain in constant memory. Do you actually see anything on the console? How far does it run? – Bergi Oct 22 '17 at 19:52
  • 7.843,3.483,+,11.326 7.843,3.482,+,11.325 Those are the last values seen. I'm trying to generate these for ml data learning. – Kannaiyan Oct 22 '17 at 20:13

1 Answers1

0

console.log is buffering and exceeding memory limits.

Using buffer flush to flush it to the system console, it will be able to proceed indefinitely.

Here is how I could solve it,

var logbuffer = require('log-buffer');
console.log('num,num2,op,result');
for (i = 9.0; i >= 0;) {
    for (j = 9.0; j >= 0;) {
        console.log(i.toFixed(3) + ',' + j.toFixed(3) + ',+,' + (i + j).toFixed(3));
        j = parseFloat((j - 0.001).toFixed(3));
    }
    logbuffer.flush();
    i = parseFloat((i - 0.001).toFixed(3));
}

I will wait for comments and any issues anyone may find before I accept it as an answer.

Thanks for all the comments in the questions which helped to debug and solve further.

Kannaiyan
  • 12,554
  • 3
  • 44
  • 83