Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Memory Settings for Wasm in WebGL

Discussion in 'Web' started by mattbrandmm, Jan 29, 2019.

  1. mattbrandmm

    mattbrandmm

    Joined:
    Jun 5, 2018
    Posts:
    90
    I am trying to optimize memory settings according to this blog:
    https://blogs.unity3d.com/2018/08/15/webassembly-is-here/

    From what I can see, there are 2 places where memory gets set in my games:
    1. in Build Settings->Player Settings->Publishing Settings->Memory Size, which is set to 256mb
    2. in the js file that I use to launch my games, where I set TOTAL_MEMORY from UnityLoader to 256mb

    Because I am using Wasm, I want to use the method described in the blog, and set the Heap Size to a smaller starting size that will grow as needed. I want to set the memory size to be lower, and I want to set a maximum heap size using this code:
    Code (CSharp):
    1. PlayerSettings.WebGL.emscriptenArgs = "-s WASM_MEM_MAX=512MB";
    However, when I try to reduce the value in #2 from 256 down to 128, 64, or 32, I get these errors in the web console:
    Code (CSharp):
    1. failed to asynchronously prepare wasm: LinkError: WebAssembly Instantiation: memory import 0 is smaller than initial 4096, got 512
    2. LinkError: WebAssembly Instantiation: memory import 0 is smaller than initial 4096, got 512
    I am hesitant to change the value in the Player Settings (#1) because I don't understand why I can't change the value TOTAL_MEMORY for UnityLoader (#2).

    Can someone help me understand why there are 2 places you can set memory, and why I cannot change the value TOTAL_MEMORY?
     
    asimdeyaf likes this.
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    W
    when using WebAssembly, the initial size of the heap is baked into the generated wasm. So you have to change PlayerSettings.WebGL.memorySize to the desired initial value. The TOTAL_MEMORY in the build json file, should not be changed.
     
    asimdeyaf likes this.
  3. mattbrandmm

    mattbrandmm

    Joined:
    Jun 5, 2018
    Posts:
    90
    Got it. Thank you!
     
  4. mattbrandmm

    mattbrandmm

    Joined:
    Jun 5, 2018
    Posts:
    90
    @Marco-Trivellato is there a recommended setting for max heap size, to be used by this?
    Code (CSharp):
    1. PlayerSettings.WebGL.emscriptenArgs = "-s WASM_MEM_MAX=512MB";
    The blog says it varies by browser. I'm not clear on what the default is, and if it should be changed or not.
     
  5. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Well, for asm.js, we had been recommending users to stay within 256MB when it comes to heap size, which cannot grow at runtime.

    When it comes to Wasm, you can start with 32 and forget about it. However, if you want to optimize your content to run on systems that don't have a lot of RAM, it might be a good idea to have a maximum size so you can test with that value. This way, you know in advance if your content will run out of memory under those circumstances.

    Does this make sense?
     
    asimdeyaf and MNNoxMortem like this.
  6. mattbrandmm

    mattbrandmm

    Joined:
    Jun 5, 2018
    Posts:
    90
    Yes, that makes sense. Thanks a lot, appreciate the info!
     
  7. Hassaan-XD

    Hassaan-XD

    Joined:
    Jul 18, 2015
    Posts:
    14
    @Marco-Trivellato and how can we set maximum size of the memory?

    I have tried (using an Editor button, confirmed it sets the memorySize to desired):
    PlayerSettings.WebGL.memorySize = 32;
    PlayerSettings.WebGL.emscriptenArgs = "-s WASM_MEM_MAX=256MB";

    but still the memory jumps to 550+ mb, I am using task manager of Chrome to view the memory usage.
    The memory usage stays below 150mb when the WebGL build is being loaded, when it's downloaded and starts the unity instance (game) the memory jumps to 550+ mb.
     
    Last edited: Oct 7, 2022
  8. K_Kuriyama

    K_Kuriyama

    Joined:
    Jul 4, 2020
    Posts:
    66
    The last question interests me too, anyone know?
     
  9. UnityProdS

    UnityProdS

    Joined:
    Jan 11, 2014
    Posts:
    41
    PlayerSettings.WebGL.memorySize is deprecated hence you are finding it has no impact.

    There doesn't seem to be any way to increase the memory size as Unity removed the feature since 2021 versions. There doesn't seem to be any documentation available too. As per Unity, the size is auto-managed by Unity however I have seen that it doesn't work. Now we are in a situation. Most of my WebGL work is stuck.
     
  10. K_Kuriyama

    K_Kuriyama

    Joined:
    Jul 4, 2020
    Posts:
    66
    @UnityProdS
    Has that item been reinstated in 2022?
     
  11. UnityProdS

    UnityProdS

    Joined:
    Jan 11, 2014
    Posts:
    41
    I don't see it in any of the documentation. Maybe Unity can respond?
     
  12. UnityProdS

    UnityProdS

    Joined:
    Jan 11, 2014
    Posts:
    41
    Unity can you please provide some guidance on increasing the memory size for webgls. My webgl output package has the following:


    upload_2022-12-2_9-14-13.png

    So in all around 23mb. All works great on desktop browsers, on mobile browser it throws the following memory error.

    Can we know how much memory Unity allocated for this output and how can we add more memory?

    Error:

    uncaught (in promise) RangeError: WebAssembly.Memory(): could not allocate memory
    at Array.unityFramework (0af6e30c-ef8f-4a8f-af13-4d7b6e476957:2:19566)
    at retail.loader.js:1:6674
    unityFramework @ 0af6e30c-ef8f-4a8f-af13-4d7b6e476957:2
    (anonymous) @ retail.loader.js:1
    Promise.then (async)
    c @ retail.loader.js:1
    (anonymous) @ retail.loader.js:2
    createUnityInstance @ retail.loader.js:2
    script.onload @ index.html?channelName=onni&Name=names&p=m:179
    load (async)
    (anonymous) @ index.html?channelName=onni&Name=names&p=m:176

    I am using Unity 2020.3.2. Webgl output settings:

    upload_2022-12-2_9-21-18.png
     
    Last edited: Dec 2, 2022
  13. UnityProdS

    UnityProdS

    Joined:
    Jan 11, 2014
    Posts:
    41
  14. UnityProdS

    UnityProdS

    Joined:
    Jan 11, 2014
    Posts:
    41
    Some more data points:

    See the JS heap snapshot below. This is from chrome - Version 107.0.5304.123 (Official Build) (64-bit) on my Desktop.

    It's strange to see TypedArrays are taking up almost all the memory allocated. What is causing this? The overall build size is not more than 23 MB, I understand after unpacking the size may go up however going up to 348 MB is ridiculous.

    This though works on my desktop chrome as its 64 bit and has more memory. This is failing badly on mobile as I am on 32-bit chrome, my game has nothing more than some textures, and a few 3d models, and the mobile browser should be able to handle it with ease.

    Completely clueless.

    upload_2022-12-3_8-4-48.png
     
    tinyant likes this.
  15. UnityProdS

    UnityProdS

    Joined:
    Jan 11, 2014
    Posts:
    41
    Ok so I tried another method. I upgraded Unity to 2022.1.23f1.

    Fortunately, it has a max and min heap size to be set. I kept min size to 32 which is Unity recommended and the max heap size to 75 mb - the lesser this memory less mobile phones will crash due to memory issues.

    This is what I get as a test result: What more I need to be doing? I have set the heap size as small as possible.

    Could not allocate memory: System out of memory!
    Trying to allocate: 15805B with 16 alignment. MemoryLabel: Serialization
    Allocation happened at: Line:69 in
    Memory overview
    [ ALLOC_TEMP_TLS ] used: 58957B | peak: 0B | reserved: 4194304B
    [ ALLOC_DEFAULT ] used: 26249400B | peak: 26249400B | reserved: 26689326B
    [ ALLOC_TEMP_JOB_1_FRAME ] used: 0B | peak: 0B | reserved: 262144B
    [ ALLOC_TEMP_JOB_2_FRAMES ] used: 0B | peak: 0B | reserved: 262144B
    [ ALLOC_TEMP_JOB_4_FRAMES (JobTemp) ] used: 0B | peak: 0B | reserved: 262144B
    [ ALLOC_TEMP_JOB_ASYNC (Background) ] used: 0B | peak: 0B | reserved: 1048576B
    [ ALLOC_GFX ] used: 14635332B | peak: 17120008B | reserved: 14637669B
    _JS_Log_Dump @ 32314e0d-8eb0-4eea-8e64-7bd06eec6268:3
     
    tinyant likes this.
  16. UnityProdS

    UnityProdS

    Joined:
    Jan 11, 2014
    Posts:
    41
    @tinyant thanks for the likes. Any guidance you can provide? Looking for some help as I am stuck with these issues.

    Thanks!
     
  17. UnityProdS

    UnityProdS

    Joined:
    Jan 11, 2014
    Posts:
    41
    Setting max heap size to 200 mb worked. I tested in multiple mobile phones including the ones having 2GB ram and seems to be working.

    For anyone struggling with the memory issue, this can help.

    However, I am now running into "Value being assigned to AudioParam.value is not a finite floating-point value." Since this thread is not for this I am sharing updates in a relevant thread.
     
  18. umitceeelik

    umitceeelik

    Joined:
    Aug 2, 2022
    Posts:
    1
    How did you set max heap size to 200mb ? I'm using unity 2021.3.7 version. Is is possible to do that on this version ?

    Thank you..
     
    Skrubl0rd likes this.
  19. sandstedt

    sandstedt

    Joined:
    May 24, 2015
    Posts:
    67
  20. hippogames

    hippogames

    Joined:
    Feb 5, 2015
    Posts:
    232
    Thanks for the info, because Unity 2021 LTS doesn't have these settings LOL. Had to migrate to Unity 2022 after spending the whole day on this in Unity 2021.