Question

I have a javascript function compiled from C with Emscritpen which use some amount of memory (about 8mb), and I need to call it often. (One call per 20 sec).

But after every call the amount of used memory increases. And eventually it starts to fail with:

Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value 100663296, (2) compile with ALLOW_MEMORY_GROWTH which adjusts the size at runtime but prevents some optimizations, or (3) set Module.TOTAL_MEMORY before the program runs.

If I increase an amount of memory, it works longer, but still fails eventually. I don't understand why EMScripten tries to enlarge a HEAP size after each call.

Some code samples:

var render = Module.cwrap('render_djvu', 'number', ['string']);

call:

FS.createDataFile("/", "test.djvu", byteArray, true, true);
removeRunDependency();
var heapPointer = render('test.djvu');
FS.truncate('test.djvu', 0);
FS.unlink('test.djvu');

UP: The problem was a memory leak in C++ code, we didn't free an allocated by C code memory. Eventually we were running out of space for heap and it required an increase. The HEAP array is persistent and is not cleared after each invocation as I initially thought.

Was it helpful?

Solution

I solved the issue. I didn't understand that if you are allocating memory in C/C++ program which will be compiled in JS. I though that on every call EMScripten will generate the new clear environment, which is not the case and the environment will be reused. So we still care about memory leaks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top