You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I've got problem with memory taken by each module instance - once memory is allocated by module, it is not returned to OS after releasing it. It can be reused by same module, but it is never released to OS, even after end of main() or emscripten_force_exit(). Even simple new/delete[] program suffers from that.
After creating and closing few instances of module, Chrome just crashes because of having more than 4GB allocated. The only flag I use is ALLOW_MEMORY_GROWTH=1 in this case, but I create modules using MODULARIZE=1 and WASM=1
Is there any way to release memory taken by module?
Try using browser's profiling tools to figure out what is holding on to the memory of a module. Once all references to it are released, it should get garbage collected automatically and freed.
When I had single Module object, i had to remove also reference to all global HEAPxxx objects and global 'buffer' object. It's annoying, but works.
But when it comes to MODULARIZE=1, I've got no idea what's going on. There is reference 'buffer' in 'system / Context' but I've got no idea what and where it is. There are some references to 'Module', but had other name in EXPORT_NAME='"test"'.
In principle MODULARIZE should keep everything in a single function scope. So unless you capture something from it and keep it alive that way, it should all be dce'able once the instance is not held on to. Unless we are somehow leaking something to the global scope?
Another possibility is that you are hitting a chrome bug, might be worth trying in other browsers and their memory devtools.
I know it might be a late but I tried to solve same issue, we are creating multiple module (about 500MB per object of memory) objects almost at the same time (deleting the old ones along the way) and the memory wasn't being freed after all the references were cleared.
Thanks to #6205 I was able to solve it because of attached gamepadconnected and gamepaddisconnected events. This may be the same cause, confirming the leak to global scope.
It seems that the pointerLockChange function would keep Module alive, and it's never removed from document. Some evidence for this is that when experiencing the leak using FIrefox, I see the following retaining paths on the ArrayBuffer:
Note the presence of mCallback. Now, if I comment out the lines indicated above which add the pointerLockChange event listener, I see the following retaining paths:
Note the absence of mCallback. In this case garbage collection does take place and the memory is freed as expected (it doesn't happen immediately, which is why In the screenshots the memory usage looks the same--but it does fall back down to the baseline memory usage in the latter case).
@tdmaienschein Event handlers can keep everything alive on the web, that's very true, yeah. You need to remove event handlers when the application can be cleaned up - that has to be done manually. Things like SDL_Quit should do it for you. If you called one of those to exit, but this still happens, that would be a bug.
Thinking on this now, I wonder if we can use WeakRef support to avoid this, by making event handlers only refer weakly to the module. I opened #9820 to think more on that.
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.
Activity
juj commentedon Aug 10, 2017
Try using browser's profiling tools to figure out what is holding on to the memory of a module. Once all references to it are released, it should get garbage collected automatically and freed.
m-karcz commentedon Aug 10, 2017
When I had single Module object, i had to remove also reference to all global HEAPxxx objects and global 'buffer' object. It's annoying, but works.
But when it comes to MODULARIZE=1, I've got no idea what's going on. There is reference 'buffer' in 'system / Context' but I've got no idea what and where it is. There are some references to 'Module', but had other name in EXPORT_NAME='"test"'.


kripken commentedon Aug 10, 2017
In principle MODULARIZE should keep everything in a single function scope. So unless you capture something from it and keep it alive that way, it should all be dce'able once the instance is not held on to. Unless we are somehow leaking something to the global scope?
Another possibility is that you are hitting a chrome bug, might be worth trying in other browsers and their memory devtools.
Mirovsky commentedon Feb 13, 2018
I know it might be a late but I tried to solve same issue, we are creating multiple module (about 500MB per object of memory) objects almost at the same time (deleting the old ones along the way) and the memory wasn't being freed after all the references were cleared.
Thanks to #6205 I was able to solve it because of attached
gamepadconnectedandgamepaddisconnectedevents. This may be the same cause, confirming the leak to global scope.jymaps commentedon Feb 22, 2019
Hi guys, can anyone post the solution to this problem? I am having the same issue of releasing memory from module.
tdmaienschein commentedon Nov 8, 2019
I think the following event listener may cause a leak:
emscripten/src/library_browser.js
Line 287 in 474daf4
It seems that the
pointerLockChangefunction would keepModulealive, and it's never removed fromdocument. Some evidence for this is that when experiencing the leak using FIrefox, I see the following retaining paths on theArrayBuffer:Note the presence of
mCallback. Now, if I comment out the lines indicated above which add thepointerLockChangeevent listener, I see the following retaining paths:Note the absence of
mCallback. In this case garbage collection does take place and the memory is freed as expected (it doesn't happen immediately, which is why In the screenshots the memory usage looks the same--but it does fall back down to the baseline memory usage in the latter case).kripken commentedon Nov 11, 2019
@tdmaienschein Event handlers can keep everything alive on the web, that's very true, yeah. You need to remove event handlers when the application can be cleaned up - that has to be done manually. Things like
SDL_Quitshould do it for you. If you called one of those to exit, but this still happens, that would be a bug.Thinking on this now, I wonder if we can use WeakRef support to avoid this, by making event handlers only refer weakly to the module. I opened #9820 to think more on that.
stale commentedon Nov 11, 2020
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.