I want to detect memory leaks by periodically logging the size of the used portion of the WASM heap. What's the easiest way of doing that?
I thought the "Rust and WebAssembly" book had some advice about this, but I can't find it.
I want to detect memory leaks by periodically logging the size of the used portion of the WASM heap. What's the easiest way of doing that?
I thought the "Rust and WebAssembly" book had some advice about this, but I can't find it.
Copyright © 2021 Jogjafile Inc.
As explained in the comments, you can get via JavaScript the total memory usage in your WASM (
WebAssembly.Memory.prototype.buffer.byteLength). This never shrinks, but if it continuously grows then you probably have a leak. You can get theWebAssembly.Memoryinstance viawasm_bindgen::memory(), and the rest can be done withwasm_bindgen:If you want a more performant implementation (this will be quite slow), or if you want a more precise metric (as said, this won't count deallocations), you can implement a global allocator:
Then call
ALLOCATOR.allocated_now()to retrieve the exact number of currently allocated bytes.