I'm writing a web server as a test project in node.js
Whats really bothering me so far is the lack of control or even awareness of memory usage. Naturally I want to cache some stuff in RAM for faster operations but how much are my objects using? There seems to be no reliable way to tell.
Best I can think up is estimating worst case scenarios, each reference 8B, each character in a string 2B but what is the base cost of an array or an object? Could be nothing or could be something depending on how they are implemented, I've got no idea.
I tried measuring the sizes of things by creating 10M of them and measuring the difference before and after with forcing GC and process.memoryUsage().heapUsed but got some really odd results, Numbers would be 10B, each character in a string 5B, an empty array 42B, and empty object 66B, I doubt its correct but its the only result I have.
At this point I'm thinking that creating a huge blob and manually converting js@v8 native types into bytes and storing them there is the only way to actually have a clue, sounds pretty lame.
Is there a practical way to actually utilize the physical memory my server near its limit without the fear of running out?