I'm trying to find the max old space size the node process has.
First I tried using the heapTotal
from process.memoryUsage()
but:
- This contains the entire heap and not the max old space size (see here for more the difference)
- I can't run it every 0 ms as it will miss memory allocated and garbage collection happen in sync operations (such as
fs.readFileSync(...)
)...
So my proposed solution that I don't know if it's right:
If I run the node process with the v8 flag --trace_gc_verbose
(which print more details following each garbage collection) which will output something like:
[7515:0x118008000] Memory allocator, used: 5400 KB, available: 4238056 KB
[7515:0x118008000] Read-only space, used: 146 KB, available: 0 KB, committed: 148 KB
[7515:0x118008000] New space, used: 212 KB, available: 810 KB, committed: 2048 KB
[7515:0x118008000] New large object space, used: 0 KB, available: 1022 KB, committed: 0 KB
[7515:0x118008000] Old space, used: 1914 KB, available: 202 KB, committed: 2204 KB
[7515:0x118008000] Code space, used: 85 KB, available: 0 KB, committed: 352 KB
[7515:0x118008000] Map space, used: 275 KB, available: 0 KB, committed: 516 KB
[7515:0x118008000] Large object space, used: 128 KB, available: 0 KB, committed: 132 KB
[7515:0x118008000] Code large object space, used: 0 KB, available: 0 KB, committed: 0 KB
[7515:0x118008000] All spaces, used: 2763 KB, available: 4240091 KB, committed: 5400 KB
[7515:0x118008000] Unmapper buffering 0 chunks of committed: 0 KB
[7515:0x118008000] External memory reported: 21 KB
[7515:0x118008000] Backing store memory: 1013 KB
[7515:0x118008000] External memory global 0 KB
[7515:0x118008000] Total time spent in GC : 2.1 ms
[7515:0x118008000] 53 ms: Scavenge stack scanning: survived_before= 155KB, survived_after= 850KB delta=81.7%
[7515:0x118008000] Fast promotion mode: false survival rate: 41%
[7515:0x118008000] 54 ms: Scavenge 3.4 (7.3) -> 3.3 (7.5) MB, 0.7 / 0.0 ms (average mu = 1.000, current mu = 1.000) allocation failure
And I extract the following line from the output:
[7515:0x118008000] Old space, used: 1914 KB, available: 202 KB, committed: 2204 KB
and sum the used (e.g. 1914 KB
) and the available (e.g. 202 KB
)
Will this be the max old space size my node process had?