Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to dynamically set --max-heap-size and --max-old-space-size in Node.js based on available memory? #4533

Open
2 tasks done
cesco69 opened this issue Jan 15, 2025 · 1 comment

Comments

@cesco69
Copy link

cesco69 commented Jan 15, 2025

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

#!/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:

  1. Is this approach correct for dynamically configuring memory in Node.js within a Docker environment?
  2. Are there any improvements or best practices I should consider for managing --max-heap-size and --max-old-space-size?
  3. 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
  • My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask
@gireeshpunathil
Copy link
Member

  1. yes
  2. 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
  3. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants