Node.js V8 Engine Memory Management
Flags for memory management within others. Trivia: ECMAScript doesn't have an API to interface program's memory management, so the V8 engines needs to handle all the memory management on it's own. https://nodejs.org/api/cli.html https://github.com/v8/v8
Node.js memory allocation
- Stack (RAM) LIFO access: is the memory set aside as scratch space for a thread of execution as storing local variables and some bookkeeping. There may be multiple stacks.
- Heap (RAM) No pattern for access: memory set aside for dynamic allocation. There is only 1 Heap per process. Slower to allocate. Responsible for memory leaks. Allocated and deallocated at runtime.
V8 allocates memory in the heap as you create new objects, or new 'pointers'.
Garbage Collection
Heap is divided into: a) Young space *Only short new objects b) Old space. *gets garbage collected in a separate cycle when full.
Since memory is limited, V8's GC scans objects allocated in memory and see if they are being used or not. Cleaned objects memory gets allocated back to the heap.
Reachability of variables and objects is done by checking whether or not another runtime object or root can get a hold of it
Logic: Newly created objects are allocated super quickly to Young Space When these objects 'survive' 2 GC scavenge cycles, they get promoted to Old Space Old Space which gets GC by Sweep in a separate cycle when full.
Node.js 1.6
Node.js sets memory limit for all objects on the heap for near 1400MB by default (at least for x64 OS).
node --max-old-space-size=1024 index.js #increase to 1gb
node --max-old-space-size=2048 index.js #increase to 2gb
node --max-old-space-size=3072 index.js #increase to 3gb
node --max-old-space-size=4096 index.js #increase to 4gb
node --max-old-space-size=5120 index.js #increase to 5gb
node --max-old-space-size=6144 index.js #increase to 6gb
node --max-old-space-size=7168 index.js #increase to 7gb
node --max-old-space-size=8192 index.js #increase to 8gb
*Note: When memory is restricted, V8 goes into emergency GC mode in order to reclaim as much heap space as possible.
Created on 6/1/2018