You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a Node.js web server built with Express.js, and I'm trying to optimize memory usage and the V8 garbage collector. My understanding is that increasing the heap size allows more memory to be allocated before triggering garbage collection (GC), which can improve performance in some cases.
To achieve this, I wrote a Bash script to dynamically configure --max-heap-size and --max-old-space-size based on the available memory in the Docker container. Here is the script:
/start-node.sh
#!/bin/sh
# memory limint on Docker container
MEMORY_LIMIT=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)
# memory limint in MB
MEMORY_LIMIT_MB=$((MEMORY_LIMIT / 1024 / 1024))
# if memory limint is too bigger (9223372036854771712 is no limit), set to 2 GB
if [ "$MEMORY_LIMIT" -gt 9223372036854771712 ]; then
MEMORY_LIMIT_MB=2048
fi
# heap is 75% of total
HEAP_SIZE_MB=$((MEMORY_LIMIT_MB * 75 / 100))
# Old Generation is 60-80% of heap
OLD_SPACE_SIZE_MB=$((HEAP_SIZE_MB * 70 / 100))
echo "Memory limit: ${MEMORY_LIMIT_MB} MB"
echo "Setting max heap size to: ${HEAP_SIZE_MB} MB"
echo "Setting max old space size to: ${OLD_SPACE_SIZE_MB} MB"
node --max-heap-size=$((HEAP_SIZE_MB * 1024)) --max-old-space-size=${OLD_SPACE_SIZE_MB} ./server.js
My Questions:
Is this approach correct for dynamically configuring memory in Node.js within a Docker environment?
Are there any improvements or best practices I should consider for managing --max-heap-size and --max-old-space-size?
Is it safe to assume that allocating 75% of the total memory to the heap and 70% of the heap to the old generation is a good rule of thumb?
Minimal Reproduction
No response
Output
No response
Before You Submit
I have looked for issues that already exist before submitting this
yes - ideally, tune it so as to avoid / minimise full gc. for this, the old space should not be used much. and for this, the workload characteristics need to be monitored and the numbers adjusted
it depends. if the process is making use of a lot of off-heap (native) memory, then this can be tight. a good number can only be arrived at after studying the workload nature.
Node.js Version
23
NPM Version
11
Operating System
Linux
Subsystem
v8
Description
I have a Node.js web server built with Express.js, and I'm trying to optimize memory usage and the V8 garbage collector. My understanding is that increasing the heap size allows more memory to be allocated before triggering garbage collection (GC), which can improve performance in some cases.
To achieve this, I wrote a Bash script to dynamically configure
--max-heap-size
and--max-old-space-size
based on the available memory in the Docker container. Here is the script:/start-node.sh
My Questions:
Minimal Reproduction
No response
Output
No response
Before You Submit
The text was updated successfully, but these errors were encountered: