Skip to content

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

Description

@Asaf-Federman

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.

Activity

  1. bnoordhuis commented on Mar 15, 2025

    @bnoordhuis
    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.

  2. Asaf-Federman commented on Mar 16, 2025

    @Asaf-Federman
    ContributorAuthor

    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!

  3. michaelper22 commented on Mar 16, 2025

    @michaelper22

    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?

  4. bnoordhuis commented on Mar 16, 2025

    @bnoordhuis
    Member

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

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

  5. Asaf-Federman commented on Mar 16, 2025

    @Asaf-Federman
    ContributorAuthor

    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

  6. bnoordhuis commented on Mar 17, 2025

    @bnoordhuis
    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?

  7. Asaf-Federman commented on Mar 18, 2025

    @Asaf-Federman
    ContributorAuthor

    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?

  8. bnoordhuis commented on Mar 19, 2025

    @bnoordhuis
    Member

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

  9. Asaf-Federman commented on Mar 20, 2025

    @Asaf-Federman
    ContributorAuthor

    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!

  10. mlc-mlapis commented on Apr 22, 2025

    @mlc-mlapis

    @bnoordhuis @Asaf-Federman You are right. The containerized environments and their dynamic ability to scale the amount of memory resources for each container are a long-term, underutilized potential, especially if those environments can scale the allocated memory in minutes and charge based on exact consumption. It is a growing, serious disadvantage of the Node.js platform.

  11. Asaf-Federman commented on May 21, 2025

    @Asaf-Federman
    ContributorAuthor

    I'm still working on the percentage implementation for --max-old-space-size. A functional version is now complete, but it requires code cleanup, proper testing, etc before a pull request can be submitted.

  12. ahmad-asmar commented on Nov 13, 2025

    @ahmad-asmar

    @Asaf-Federman, will the percentage implementation need a pod restart? The The latest Kubernetes version allows in-place resource modifications without recreating the pod. https://kubernetes.io/docs/tasks/configure-pod-container/resize-container-resources/

  13. Asaf-Federman commented on Nov 13, 2025

    @Asaf-Federman
    ContributorAuthor

    @Asaf-Federman, will the percentage implementation need a pod restart? The The latest Kubernetes version allows in-place resource modifications without recreating the pod. https://kubernetes.io/docs/tasks/configure-pod-container/resize-container-resources/

    While I haven't tested this specific scenario, a container/process restart would likely still be required.

    The reason is that the percentage calculation happens before the Node.js process is started, and the resulting fixed value (in MB) is what's actually passed to the V8 engine via the --max-old-space-size flag. The V8 engine itself isn't aware of the percentage or the cgroup limit, only the fixed value it was given at launch.

  14. ahmad-asmar commented on Dec 17, 2025

    @ahmad-asmar

    @Asaf-Federman, will the percentage implementation need a pod restart? The The latest Kubernetes version allows in-place resource modifications without recreating the pod. https://kubernetes.io/docs/tasks/configure-pod-container/resize-container-resources/

    While I haven't tested this specific scenario, a container/process restart would likely still be required.

    The reason is that the percentage calculation happens before the Node.js process is started, and the resulting fixed value (in MB) is what's actually passed to the V8 engine via the --max-old-space-size flag. The V8 engine itself isn't aware of the percentage or the cgroup limit, only the fixed value it was given at launch.

    If that were possible, it would have been a very cool feature to use with Kubernetes VPA. As soon as VPA starts taking advantage of the in-place resource modifications feature, we can use VPA in auto mode that would change resources without pods restarting, and Node.js/V8 would adapt the percentage to the new resources requests without restarting the application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions