nodejs / nodejs/node-gyp

Allocating more than 4GB of memory

Open
#2,902 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10.7k
Forks
1.9k
Avg merge
1d 4h
Merged PRs (30d)
5

Description

I have a c++ function that allocates more than 4GB of memory and sends this to node as an array buffer:

void AllocateMemory(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();
    size_t size = 8ull * 1024ull * 1024ull * 1024ull;  // 8GB

    printf("Allocating %zu bytes!!!!\n", size);

    void* memory = std::malloc(size);

    printf("Succeeded\n");

    if (memory) {
        // Create a Node.js Buffer that points to the allocated memory
        Napi::Env env = info.Env();
        Napi::ArrayBuffer buffer = Napi::ArrayBuffer::New(env, memory, size);

        // Resolve the promise with the allocated buffer
        info[0].As<Napi::Function>().Call(env.Global(), { buffer });
    } else {
        // Reject the promise if memory allocation fails
        Napi::Error::New(env, "Memory allocation failed").ThrowAsJavaScriptException();
    }
}

The allocation itself succeeds but, line Napi::ArrayBuffer buffer = Napi::ArrayBuffer::New(env, memory, size); fails with the error message Error: Cannot create a Buffer larger than 0x100000000 bytes

Is this something that is generally not supported using node-gyp?

However, in node itself it is not a problem to run something like

const eightGBBuffer = new ArrayBuffer(8 * 1024 * 1024 * 1024); // 8GB
console.log(`8GB buffer allocated: ${eightGBBuffer.byteLength} bytes`);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The reproducer is the C++ N-API function calling Napi::ArrayBuffer::New; start there and compare its size limit with the JavaScript ArrayBuffer example. Done means determining whether this native-addon path is supported and documenting the supported behavior or confirmed limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, node.js
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.