emscripten-core / emscripten-core/emscripten

Efficiently using memory with wasm/asmjs?

Open
#13,117 2 comments 0 reactions 0 assignees View on GitHub
wontfix
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Hello, I am porting cross platform code to Webassembly with USE_PTHREADS=1, PTHREAD_POOL_SIZE=8, TOTAL_MEMORY/INITIAL_MEMORY=64MB. While running the code in chrome as browser extension, I observe that in chrome task manager that the memory usage of extension is around 350MB even when it is in idle state. I made sure that none of the extension JS code is using that memory and also decreasing PTHREAD_POOL_SIZE is not helping. I checked most of the memory is allocated at the time of wasm load itself much before I invoke my native methods.

Just trying to figure out if there are any hidden gotchas or an advisable way to use memory efficiently while compiling for WASM?
I have below code in the generated JS file.

```
var PAGE_SIZE = 16384;
var WASM_PAGE_SIZE = 65536;
var buffer, HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64;

function updateGlobalBufferViews() {
Module["HEAP8"] = HEAP8 = new Int8Array(buffer);
Module["HEAP16"] = HEAP16 = new Int16Array(buffer);
Module["HEAP32"] = HEAP32 = new Int32Array(buffer);
Module["HEAPU8"] = HEAPU8 = new Uint8Array(buffer);
Module["HEAPU16"] = HEAPU16 = new Uint16Array(buffer);
Module["HEAPU32"] = HEAPU32 = new Uint32Array(buffer);
Module["HEAPF32"] = HEAPF32 = new Float32Array(buffer);
Module["HEAPF64"] = HEAPF64 = new Float64Array(buffer)
}

var INITIAL_TOTAL_MEMORY = Module["TOTAL_MEMORY"] || 67108864;

if (!ENVIRONMENT_IS_PTHREAD) {
wasmMemory = new WebAssembly.Memory({
"initial": INITIAL_TOTAL_MEMORY / WASM_PAGE_SIZE,
"maximum": INITIAL_TOTAL_MEMORY / WASM_PAGE_SIZE,
"shared": true
});
buffer = wasmMemory.buffer;
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.