I’m working with the following code:
for (var x = 1; x < 10000000; x++) {
count++
myObj.name = count
ch.sendToQueue(queueName, new Buffer(JSON.stringify(myObj)));
}
If I comment out the sendToQueue command it finishes without error, if I don’t comment it I get the error: FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory. I believe this is happening because it’s adding my sendToQueue call to a callback queue, these build up and take up memory until there is no memory left.
Is there a way to force the callbacks to run synchronously so they don’t build up?
I’m guessing this may not be the best use case for Node/JavaScript and would love confirmation if that’s the case.
I understand one solution would be to break the loop up into multiple smaller loops, in this particular use case I can’t do that, it needs to be a single long running loop.