I’m facing an issue with my WebGL build. It perfectly works until a specific calcul and crashes with an “Out of memory” issue. I’ve red that memory is increased till 2GB if needed. I suppose It crashes because my build needs more than 2GB but that seems huge juste for my calculations… (mainly boolean operations).
How to know how much memory I need ? And how to know how munch memory my build uses before this calculation starts ?
Can someone help ?
I use Unity 2019.4.1f1
That’s the issue displayed in Chrome’s console :
Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value 2141519872, (2) compile with -s ALLOW_MEMORY_GROWTH=1 which allows increasing the size at runtime, or (3) if you want malloc to return NULL (0) instead of this abort, compile with -s ABORTING_MALLOC=0
Maybe you should put the memory according to the general web browser requirements, I think 2 GB of RAM for the web browser is too much. Try with 512MB.
And try not to use fancy graphics, maybe it’ll crash too.
Thanks for your repsonses. Unfortunatly I can’t custom the memory as in Unity 2017. This option has been removed for Unity 2019 and 2020. Now the memory needed is automatically calculated.
Is it upgraded project?
Unity 2019 and up uses the option of “compile with -s ALLOW_MEMORY_GROWTH=1 which allows increasing the size at runtime”
So your project already might exceeded the maximum.
It’s better to use the dynamic size option, as it starts with small footprint(faster loading time) and increase when needed.
But if you want to have a constant size, you can edit the “webGLEmscriptenArgs” in the “ProjectSettings.asset” file,
by editing the file, or by setting it from code by setting “PlayerSettings.WebGL.emscriptenArgs”.
The value should be
“-s ALLOW_MEMORY_GROWTH=0 -s TOTAL_MEMORY=THE_MEMORY_SIZE”
While “THE_MEMORY_SIZE” should be the size in bytes. So for 2GB it would be 2147483648. (Which is already larger than the limit that you had in your post 2141519872).
You should check how you can reduce your memory size, or if you have a memory leak…
Thanks for your response. I don’t need a constant size, the dynamic size option is OK for me. I would like to know how much memory I need and how much memory is used during my build. Is that possible ?
I’m working on reduce my calculation but I still have this out of memory issue for now.
Try hooking into the Unity profiler on the web page to get a grasp of what is taking up memory in the build. That should hopefully give a clue towards what is blowing up the build sizes. See Unity - Manual: Memory Profiler module
When we add Standard and Standard Specular shader at “Always Included Shaders”
When we load assets dynamically
In order to solve the first problem, we create a script to remove the unnecessary shader variants from Standard shaders via OnProcessShader. In our case, we removed FOG_… and LIGHTMAP_… keywords among others.
After stripping shader variants, memory in the task manager changed from 1.4GB to 650MB.
We need to include the shaders in the “Always Included Shaders” because we load asset bundles with materials.
Second problem, as we load content dynamically in our scene, memory is growing, but not released. To solve this, we have to reload the main scene. This way, memory is released and we avoid the “Out of Memory” browser message.