So I have an express server that tries to send back a lot of JSON data (625,763,181 chars if stringified) with res.send(data)
. This crashes the application with
`FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory``
(more info can be found on my last question on this topic)
Here's a few snapshots of the heap before and after it tries to send the data:
As you can see the memory goes from 28MB to 700MB and 97% of it is allocated as strings (and inspecting it I see that it's the JSON data stringified).
So my question is: Am I doing anything wrong here, should I compress the JSON somehow or use something other than res.send
? or can I just not send that much data?
OBS: I know I can add --max-old-space-size=8192
but I'm trying to host the project on a server with limited space that runs out with or without that flag.
EDIT: I'm leaving this up because there might be optimisations I hadn't thought of, and I'm really interested. But I found a bug in my data causing it to grow in O(n*m) instead of just O(n+m), reducing the size from 625,763,181 chars to 131,700 chars.