Skip to content

Emscripten causes memory leaks when attaching events listeners to the process #18723

@jeswr

Description

@jeswr

If I execute emcc helloworld.cpp -s MODULARIZE=1 -o dist/index.js on the following file

#include <stdio.h>

int main() {
  return 0;
}

then if I call the generated index.js module as follows

const helloWorld = require('./dist')

async function main() {
  for (let i = 0; i < 10_000; i += 1) {
    await helloWorld();
  }
}

each instantiation takes up an additional ~58KB of memory and the time taken to run each execution slowly decreases. For 10_000 calls it takes approximately 4min and 0.5GB of memory is consumed.

In contrast if I instantiate the Web-assembly file directly

const fs = require('fs');
const path = require('path');

async function main() {
  for (let i = 0; i < 10_000; i += 1) {
    const module = await WebAssembly.instantiate(fs.readFileSync(path.join(__dirname, 'dist', 'index.wasm')));
    await module.instance.exports.main()
  }
}

all 10_000 calls run in about 4 seconds and take up no memory (other than the 4MB of memory that the process had consumed before the first instantiation).

I have created a minimal reproduction here and this was first discovered as a problem here.

Version of emscripten/emsdk:
3.1.20

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions