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

Support percentage-based --max-old-space-size option for dynamic memory allocation #57447

Open
Asaf-Federman opened this issue Mar 13, 2025 · 9 comments
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@Asaf-Federman
Copy link

What is the problem this feature will solve?

Currently, --max-old-space-size requires a fixed memory value in MB. However, in containerized environments (Docker, Kubernetes, etc.), available memory can vary. A percentage-based option would allow Node.js to dynamically adjust its memory usage based on the environment, reducing the risk of OOMKills while optimizing performance.

Why is this needed?

  • Improves compatibility with containerized environments like Kubernetes, where memory limits are dynamic.
  • Prevents over-allocating memory and OOMKills when limits change.
  • Simplifies memory management without requiring external scripts to calculate heap size dynamically.

What is the feature you are proposing to solve the problem?

Introduce a new syntax for --max-old-space-size, allowing it to accept percentages of total system memory (e.g., --max-old-space-size=80%).

For example:
node --max-old-space-size=80% app.js

If the system has 4GB RAM, this would set 3.2GB (80%) as the old space size.
If the system has 1GB RAM, it would set 800MB (80%).

What alternatives have you considered?

  • Users must manually set a fixed value.
  • Some use external scripts (os.totalmem()) to compute the size dynamically.
@Asaf-Federman Asaf-Federman added the feature request Issues that request new features to be added to Node.js. label Mar 13, 2025
@github-project-automation github-project-automation bot moved this to Awaiting Triage in Node.js feature requests Mar 13, 2025
@bnoordhuis
Copy link
Member

--max-old-space-size is a V8 flag, not a Node.js flag. It's technically possible to intercept and rewrite V8 flags but we've never done that before and personally I see no reason to start now.

Also, percentages seems like a bad idea in general. What happens when I start two node processes with --max-old-space-size=80%? Boom, is what.

Not a hypothetical. The flag is allowed in NODE_OPTIONS. Child processes inherit it.

@Asaf-Federman
Copy link
Author

Hi bnoordhuis,

I appreciate your points about the potential complexities of using --max-old-space-size with percentages, especially when dealing with multiple Node.js processes. However, I believe there are compelling reasons to consider allowing this functionality.

One significant advantage of supporting percentage values is the potential for dynamic resource allocation, similar to what's seen with Vertical Pod Autoscaler (VPA) in Kubernetes. By utilizing percentages, we can enable applications to adapt their memory usage based on the available resources at runtime. This flexibility could lead to more efficient memory management, especially in containerized environments where resource limits can change.

Moreover, allowing percentage-based configurations could help developers optimize their applications without the need to hardcode specific values, which might not suit varying production environments. It empowers users to maintain better control over their applications' memory usage, potentially improving performance and resource utilization.

Thanks for your input!

@michaelper22
Copy link

Hi bnoordhuis,

While the implications of more config permutations cannot be ignored, percentage-based is a pattern reliably utilized in production environments in other platforms, notably Java with -XX:MaxRAMPercentage=nn%. Allowing percentages would let our JS developers unify these configs across the enterprise.

Are there popular frameworks/libaries for Node which fork processes in their standard functionality?

@bnoordhuis
Copy link
Member

Are there popular frameworks/libaries for Node which fork processes in their standard functionality?

Yes, plenty. The built-in cluster module, for starters.

@Asaf-Federman
Copy link
Author

Asaf-Federman commented Mar 16, 2025

I'd like to emphasize that even with fixed values, we can still encounter similar problems.

When we set a fixed value for --max-old-space-size, if both the parent and child processes are given a substantial allocation, the combined memory usage can easily exceed the available system memory, leading to crashes or performance issues.

Moreover, this feature shouldn't introduce any regressions or breaking changes; hence, any existing codebase won't break

@bnoordhuis
Copy link
Member

I'd like to emphasize that even with fixed values, we can still encounter similar problems.

Yes, obviously.

To set expectations: don't expect anyone to work on this. Open a pull request yourself if you want to see it happen. Keep in mind there's no guarantee it's going to get merged.

In your pull request you're going to have to answer the question what 80% really means. 80% of total system memory? What if the process is in a cgroup? Does it make sense to limit yourself to 80% of the cgroup's memory limit? If yes, why? If not, why not?

@Asaf-Federman
Copy link
Author

I'd like to emphasize that even with fixed values, we can still encounter similar problems.

Yes, obviously.

To set expectations: don't expect anyone to work on this. Open a pull request yourself if you want to see it happen. Keep in mind there's no guarantee it's going to get merged.

In your pull request you're going to have to answer the question what 80% really means. 80% of total system memory? What if the process is in a cgroup? Does it make sense to limit yourself to 80% of the cgroup's memory limit? If yes, why? If not, why not?

Thanks, @bnoordhuis , for your feedback!

One last question, if I may: would it be more fitting to try to contribute this effort to the V8 engine instead of Node.js?

@bnoordhuis
Copy link
Member

V8 is the better place, the question is if they'd accept it.

@Asaf-Federman
Copy link
Author

I reached out to the V8 community regarding this feature, and they indicated that it would be more appropriate to contribute it to the Node.js project. You can find the relevant discussion here.

In light of this feedback, I intend to proceed with contributing this feature to the Node.js project. Any assistance or guidance on this would be greatly appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js.
Projects
Status: Awaiting Triage
Development

No branches or pull requests

3 participants