Skip to content

Ability to constrain the total heap used by v8 and Node.js (--max-heap-size) #24225

@giltayar

Description

@giltayar

Is your feature request related to a problem? Please describe.

Running NodeJS applications in a memory-constraining environment like Kubernetes is today problematic, because there is no way to tell NodeJS (and the v8 GC) how much total memory it has to work with. --max-old-space-size is a good start, but due to the way Buffer uses memory allocated outside of v8's heap (called "external memory"), this is not enough.

The problem in essence is this: when running under a memory constraint (i.e. docker/k8s limiting the process' memory), one can specify --max-old-space-size that is below the memory constraint, have memory that is below that usage, and yet the process still gets OOMKilled, because most of that memory lives in Buffer-s, and thus in the external heap, which v8 does not know about and thus does not feel like it needs to trigger a GC.

I created a Github repo that reproduces this problem: https://github.com/giltayar/node-memory-constraining-problem. Follow the README to reproduce the problem and to see a more in depth examination of this problem.

Describe the solution you'd like

I would like a --max-heap-size parameter to NodeJS that constrains v8's heaps and the external heap to not pass a specified limit.

This solution was proposed by @psmarshall and @bmeurer from the v8 team after discussing the problem with them.

Describe alternatives you've considered

A --max-external-heap-size parameter that constrains the external memory size used by Buffer and others.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.memoryIssues and PRs related to the memory management or memory footprint.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions