6

How would one simply clear up all of the memory used by a WebAssembly instance. But still allow for the WASM binary to be run again.

Essentially, I have a WASM operation that uses a lot of memory, and if memory runs out (too much memory has been allocated and the system cannot give anymore), I simple want to reset the program; by clearing all of the memory used, but also allow for the program to be run again... Is there a way to do this via the WebAssembly JavaScript API, as my searches online have yielded no results. Would one just delete the shared array buffer, and reallocate that to the same variable - I do not know?

Any help would be much appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Achmed
  • 313
  • 3
  • 11

1 Answers1

1

The only way to clear up the Wasm heap is to allow all instances of the wasmInstance to be orphaned and allow JavaScript to garbage collect the wasmInstance (see https://github.com/WebAssembly/design/issues/1396)

Once the wasmInstance is GC'd the wasm heap associated with that instance will also be deleted. Unfortunately this means going through your app and ensuring that there are no closures or accidental memory leaks of wasmInstance.

There is no current way to shrink the wasm heap, however there is this experimental proposal for memory.discard which works on some nightly builds of firefox to shrink or reclaim memory once freed

Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178