-
Notifications
You must be signed in to change notification settings - Fork 694
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
Release and reacquire memory #1396
Comments
I could not find any discussion on releasing and re-acquiring memory, hence a new issue. |
Disposing of the |
@tlively could you provide a sample of code? Or maybe a demo on JSFiddle? |
async function compile() {
const response = await fetch('module.wasm');
const buffer = await response.arrayBuffer();
const module = await WebAssembly.compile(buffer);
return module;
}
function getInstanceSync(module, importObject = {}) {
return new WebAssembly.Instance(module, importObject);
}
function getInstanceAsync(module, importObject = {}) {
return WebAssembly.instantiate(module, importObject);
}
const mod = await compile();
const instance1 = getInstanceSync(mod);
const instance2 = getInstanceSync(mod);
const instanceN = getInstanceSync(mod); |
@MaxGraey It works well in Firefox, but in Chrome, I am getting a following error in the latest Google Chrome:
It has been mentioned before - Chrome does not allow synchronous compilation of large WASM for some reason. Is here any Chromium developer? Could you fix it? I think if someone decides to do it on the main thread, they already know what they are doing. My app www.Photopea.com often uses 4 - 8 GB of RAM, it often blocks the main thread for 5 - 10 seconds or more (the user sits and waits), so I don't think compiling my 30 kB WASM on the main thread would ruin the user experience. |
The synchronous compilation of large modules is discouraged exactly for the reason you mentioned above - i.e. it leads to a pretty bad user experience, and this limit is unlikely to be changed. If you have Chrome specific concerns, please follow up on the Chromium issue tracker. |
But why falls the |
There's a few reasons this isn't done, one is that it would break existing sites. One of the leading philosophies of browser design is as much as possible to avoid breaking sites unneccessarily. Another though is just that interrupting JS execution by itself is not safe, the reason If JS were recreated today, blocking for long periods probably wouldn't be allowed on the main thread. But unfortunately JS is not a new language, it has to support all of the old stuff in addition to new stuff so it's not super viable to just make large changes like that.
Although I do agree that restricting |
The restrictions on the Instance (and even the Module) constructor were rather controversial, even among Chromium devs. The web platform team insisted, while others thought there is no coherent reason to do so, like pointed out here as well. But as @dtig mentioned, the Chromium tracker is the right place to lobby for lifting the restriction. |
Hi everyone, wonder if you found a solution to this? I have an issue where new/delete of large arrays is causing memory fragmentation due to limitations in the wasm memory model (see #1397 ) we need a way to collect all and allow application memory to shrink when done with long running calculations |
There's a few different issues mentioned here, and depending on what you're doing currently the solution may be different. If the calculations are across instances, @tlively's suggested method of disposing instances seems like a good approach. For discarding application memory, please follow this comment in the issue you linked for a memory.discard discussion + prototype. The Chrome limitation in synchronous compile is raised to 8KB, but it would still be for user experience to block the main thread for large module compilation. |
Disposing instances (see comment here ) seems like it could work but what’s involved? Drop every reference to the module? Still trying to figure out how this works. Thanks! |
Not to the module, but all refs to the instance. If you also drop all refs to the module, you'll have to go through the whole bootstrapping again. By keeping the module around, you can get much faster to a new instance. |
Thank you that’s V. helpful |
Can confirm. Once all instances of wasmInstance are dropped and the instance is GC'd by JavaScript, the wasm heap is also deleted and memory returned to the host OS |
Originally posted by @photopea in #1300 (comment)
My visitors spend hours on my website without reloading it.
My wasm program runs once every 5 minutes, for 0.5 seconds each time. It is called by a synchronous JS function, which expects the output right after the wasm finishes. During the execution, the wasm program needs 700 MB of RAM.
I would like to release these 700 MB when the wasm program does not run. What JS object should I release (remove references to)? Next time my JS function runs, I want to run the wasm program again (in a synchronous way).
I was thinking about recompiling WASM each time my JS function runs, but all current browsers compine WASM asynchronously. Is there anything else I can do?
These lines do nothing:
The text was updated successfully, but these errors were encountered: