Search Unity

Question WebGL "out of memory" issue

Discussion in 'Web' started by Camille_Gobled, Sep 22, 2020.

  1. Camille_Gobled

    Camille_Gobled

    Joined:
    Sep 3, 2020
    Posts:
    9
    Hi everyone !

    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

    Thanks for your time ! :)

    (Sorry about my English, I'm French)
     
  2. atorres1986

    atorres1986

    Joined:
    Nov 24, 2016
    Posts:
    2
    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.

    Hope it helps a little.
     
  3. ajeet09

    ajeet09

    Joined:
    May 18, 2020
    Posts:
    4
    Is there an option in unity 2019.4 to provide our custom memory, as it does in unity 2017?
     
  4. Camille_Gobled

    Camille_Gobled

    Joined:
    Sep 3, 2020
    Posts:
    9
    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.
     
  5. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589
    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...
     
  6. Camille_Gobled

    Camille_Gobled

    Joined:
    Sep 3, 2020
    Posts:
    9
    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.
     
  7. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
  8. Camille_Gobled

    Camille_Gobled

    Joined:
    Sep 3, 2020
    Posts:
    9
    Thank you everyone, I've found the issue !
     
  9. SIMSA-TeoWin

    SIMSA-TeoWin

    Joined:
    Nov 29, 2016
    Posts:
    1
    Hello, we are facing the same issue for a long time.
    Can you tell what the issue was in your case?
    Thanks!
     
  10. jsr2k1

    jsr2k1

    Joined:
    Aug 11, 2010
    Posts:
    118
    Same here! Our WebGL compilation needs 2GB on Chrome and we don't know why!
     
    nbrown-tbot likes this.
  11. nbrown-tbot

    nbrown-tbot

    Joined:
    Jun 26, 2020
    Posts:
    2
    What was the cause of the issue and how did you fix it?
     
  12. jsr2k1

    jsr2k1

    Joined:
    Aug 11, 2010
    Posts:
    118
    We have found two problems mainly:
    - 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.

    Hope this helps!