Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Wasm heap growth

Discussion in 'WebGL' started by ArtyomOverchuk, Nov 15, 2019.

  1. ArtyomOverchuk

    ArtyomOverchuk

    Joined:
    Nov 5, 2018
    Posts:
    6
    We have pretty big project with big number of Asset Bundles for Webgl. They can gradually load as the player moves. So memory usage can grow slow with time. But in one moment I can see significant spike. Memory may increase from 700 megabytes to 1100 megabytes. These are the build parameters new and old (commented out) build settings. The heap behavior has not changed with the change of settings. Why? Shouldn't memorySize be responsible for the size of the heap increase?
    Code (CSharp):
    1.         PlayerSettings.WebGL.linkerTarget = WebGLLinkerTarget.Wasm;
    2.         PlayerSettings.WebGL.memorySize = 32;
    3.      
    4.         /*Debug.Log("Build version set to WebAssembly/Asm.js");
    5.         PlayerSettings.WebGL.linkerTarget = WebGLLinkerTarget.Both;
    6.         PlayerSettings.WebGL.memorySize = 256;*/
     
    Last edited: Nov 15, 2019
  2. Uli_Okm

    Uli_Okm

    Joined:
    Jul 10, 2012
    Posts:
    94
    If you are in Unity 2019, this setting was deprecated (and also hidden in the inspector). Unity now always start with the same memory heap size, and when needed it will grow. The problem is: this grow amount is decided by Unity, I coudn't find where they explained this behaviour, but I saw here in the forum.
     
    ArtyomOverchuk likes this.
  3. ArtyomOverchuk

    ArtyomOverchuk

    Joined:
    Nov 5, 2018
    Posts:
    6
    Thanks for the answer. If you change the memorySize, you can see that this affects the memory, if you check on an empty build. So it’s hidden, but not entirely deprecated. I saw the discussions here and it seemed to me that the memory is allocated in blocks the size of which is set in memorySize. But in the build it seems that the heap is always a power of two. I can’t get an exact answer.
     
  4. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    Last time I checked, the heap grows by a power of two up until ~1GB, then in roughly 100MB increments to a current hard limit of 2032MB minus 64KB due to various reasons (although your browser may not necessarily allow the page to reserve the full 2GB). This logic comes from the underlying toolchain Emscripten.

    I'd link the documentation but unfortunately the actual memory growth pattern isn't part of it. If you do a search for the comment double until 1GB around line 1018, the rest of the comments around it should explain the growth process.
    https://github.com/emscripten-core/emscripten/blob/1.38.11/src/preamble.js#L1018

    This is actually slightly different in the newer version of the toolchain (Wasm itself handles memory growth) but Unity uses an older version.
     
  5. ArtyomOverchuk

    ArtyomOverchuk

    Joined:
    Nov 5, 2018
    Posts:
    6
    Great, thanks
     
  6. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595
    I'm finding that it jumps up by 500Mb after it hits 1Gb.
    Does anyone know of a way to control that, I only need very slightly over 1Gb at the moment.
    Hopefully I can reduce that, but I certainly don't need 1.5Gb