emscripten-core / emscripten-core/emscripten

Emscripten causes memory leaks when attaching events listeners to the `process`

Open
#18,723 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

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

```cpp
#include

int main() {
return 0;
}
```

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

```ts
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

```ts
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](https://github.com/jeswr/emscripten-memory-leak) and this was first discovered as a problem [here](https://github.com/rla/npm-swipl-wasm/issues/23).

**Version of emscripten/emsdk:**
3.1.20

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.