I have a 70 MB JSON file, which I read from a Node.js script and assign to a variable.
console.log(process.memoryUsage());
let data=require('../newJSON.json');
console.log(process.memoryUsage());
Output:
{ rss: 28184576,
heapTotal: 6283264,
heapUsed: 4199672,
external: 8252 }
{ rss: 724721664,
heapTotal: 695595008,
heapUsed: 663708016,
external: 8252 }
It seems that 70 MB JSON takes 632 MB of memory. I am interested in understanding how does JSON is stored into memory by Node Js/ Javascript?
First off, JSON is just a string representation of objects. There is nothing special about "JSON objects" -- the JSON parser parses the JSON string and creates regular JavaScript objects from it. This:
var a = JSON.parse('{"foo": "bar"}');
and this:
var a = new Object(); a.foo = "bar";
are completely equivalent.
Object storage in memory is complicated, because modern JavaScript engines have pretty nifty optimizations for various different circumstances depending on what your code is doing.
JSON string length and size of the corresponding object in memory are not strictly correlated; in most cases the JSON representation is expected to be smaller, sometimes by a lot. E.g. for the innermost nesting of your example: "a":0,
takes 6 bytes, whereas for one more property in the created object, you need:
On a 64-bit platform, that adds up to ~40 bytes.
If you look at an entire object of similar shape: {"a":0,"b":1}
is 13 characters, whereas the memory requirement is:
In total, 26 pointers or 208 bytes.
Lastly, there's a chance that some of the memory usage you see is from temporary objects that the GC will clean up over time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With