emscripten-core / emscripten-core/emscripten

Opencv.js --threads how to compleatly release memory when refreshing page?

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

Description

Hi,

How to completely release memory when an application terminates? For example, when refreshing a page or moving to the another page?

The error (not enough memory) only occurs when refreshing a page with the **multi-threaded version** of opencv (build with --threads flag) and does not occurs with the single-threaded version. And only on devices with 3 GB of RAM.

I tried release memory like this, but it didn't help:
```
window.onbeforeunload = function()
{
if (cv.PThread !== undefined)
{
cv.PThread.terminateAllThreads();
cv.__emscripten_thread_free_data();
}
}
```
It seems that the memory is not release or it does not release immediately on page refresh. Therefore, when opening the application again in the same tab, an error occurs.

P.S.
I had exactly the same error with Tensorflow.js. It was successfully resolved after memory was released on the window.onbeforeunload event. There the memory release function looks like this:
```
TFLiteWebModelRunner.prototype.cleanUp=function()
{
if(null!=this.cppClassifier)
{
for(var a=$jscomp.makeIterator(this.memOffsetsToFree),b=a.next();!b.done;b=a.next())
this.module._free(b.value);
this.cppClassifier.delete()
}
};
```

Is there the same function in opencv.js?
Is there a way to simply reset the wasm memory when the application terminates?

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.