Closed breavyn closed 2 years ago
Good pick up. I'll look into this. Maybe wasm-bindgen doesnt auto free the memory when a key goes out of scope.
I'll do some tests with calling key.free()
I can confirm that the below code doesnt exhibit any memory leak. Maybe we can look into making some further changes to wasm-bindgen to facilitate WASM GC when JS objects go out of scope.
const { PrivateKey } = require('../../pkg/node/bsv_wasm');
let now = Date.now()
const target = 15_000_000;
for (let index = 0; index < target; index++) {
const key = PrivateKey.fromRandom();
key.free(); // Release the WASM memory
}
console.log(`Took ${Date.now() - now}ms for ${target} iterations`);```
This is actually planned to be supported by wasm-bindgen in future when Weak References are added to most browsers. https://rustwasm.github.io/wasm-bindgen/reference/weak-references.html
So does this mean that I could build the lib for node.js today using the --weak-refs
flag?
Yep, I’ve been looking into a way to pass the flag to the wasm-bindgen-cli so we can bypass wasm-pack. I just haven’t had enough time to completely research it yet. 100% possible though
Tested with Node v16.10.0 on Linux.
If we take a simple example
and run it, after 5 minutes we've nearly hit the heap limit
and kaboom!