-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak when repeatedly instantiating a WASM module #3130
Comments
Unfortunately there's not really anything that can be done about this. You'd need some sort of destructor for the entire instance akin to "program shutdown" which is not implemented. Such a destructor would be responsible for clearing this global. If you're creating tons of wasm instances I'd recommend not storing things in globals and instead using objects which should have destructors to trigger things correctly with finalizers. |
Can For my use-case, it's not really about creating tons of instances, more when an instance with a lot of heap values attached crashes and is restarted. |
What you probably want is to have all the intrinsics and such that wasm-bindgen uses to be closure state in the initialization function. That way they'd all get gc'd with everything else when they're no longer referenced (e.g. after something crashes and has been replaced). |
That's exactly it, yeah; as far as I can tell, there's not really any reason the various export function initSync(module) {
let globalsObj = { heap: /* do heap init */ };
const imports = getImports(globalsObj);
// ...
const instance = new WebAssembly.Instance(module, imports);
globalsObj.wasm = instance.exports;
return finalizeInit(instance, module);
} |
Describe the Bug
The
heap
array grows without bounds when repeatedly instantiating a WASM module and calling any function that adds a JS object to the heap.Steps to Reproduce
init
the wasm module and call this function with a new JS object each time, e.g.while (true) { const instance = initSync(module); instance.set_value(new Object()); }
.Expected Behavior
Memory usage to stay level while running.
Actual Behavior
Memory usage continuously increases while running.
Additional Context
In the
init
method, even thoughwasm
gets changed (effectively invalidating the old instance, as now the module-level functions will be referring to a different instance when usingwasm
), the heap isn't re-initialized. So all the values that were stored in the old instance continue to be held onto, even when they're effectively unusable by the new instance. This can be observed when, e.g., the Rust code uses a panic handler that callsthrow_str
, where the calling JS code will re-init the module after a panic (similar to Erlang behaviour).The text was updated successfully, but these errors were encountered: