Search Unity

Unity 2020.3.4f1 (LTS) WebGL Memory & ReferenceError

Discussion in 'Web' started by jonkuze, Apr 28, 2021.

  1. jonkuze

    jonkuze

    Joined:
    Aug 19, 2012
    Posts:
    1,709
    When building with compression disabled, the build completes successfully but when trying to load the WebGL Build in browser I see the following Error Cannot Enlarge Memory Arrays (below is the full console error).
    Code (JavaScript):
    1. Could not allocate memory: System out of memory!
    2. Trying to allocate: 9573980B with 16 alignment. MemoryLabel: DynamicArray
    3. Allocation happened at: Line:70 in
    4. Memory overview
    5.  
    6. [ ALLOC_TEMP_THREAD ] used: 623971B | peak: 0B | reserved: 4194304B
    7. [ ALLOC_TEMP_JOB_1_FRAME ] used: 0B | peak: 0B | reserved: 262144B
    8. [ ALLOC_TEMP_JOB_2_FRAMES ] used: 0B | peak: 0B | reserved: 262144B
    9. [ ALLOC_TEMP_JOB_4_FRAMES ] used: 0B | peak: 0B | reserved: 262144B
    10. [ ALLOC_TEMP_JOB_ASYNC ] used: 0B | peak: 0B | reserved: 262144B
    11. [ ALLOC_DEFAULT ] used: 16529610B | peak: 16529610B | reserved: 17099230B
    12. [ ALLOC_GAMEOBJECT ] used: 862556B | peak: 867968B | reserved: 867515B
    13. [ ALLOC_GFX ] used: 220959587B | peak: 220959587B | reserved: 220966560B
    When building with compression enabled (Gzip or Brotli) the build completes successfully but when trying to load the WebGL Build in browser I see the following console errors:
    Code (Boo):
    1. /Build/WebGL.asm.framework.js.gz:1 Uncaught SyntaxError: Invalid or unexpected token
    2. WebGL.asm.loader.js:1 Uncaught ReferenceError: unityFramework is not defined
    3.     at HTMLScriptElement.r.onload (WebGL.asm.loader.js:1)
    4. /Build/WebGL.asm.js.gz:1 Uncaught SyntaxError: Invalid or unexpected token
    5. WebGL.asm.loader.js:1 [UnityCache] 'http://localhost/Build/WebGL.data.gz' successfully downloaded and stored in the indexedDB cache
    Please advise...
     
    Last edited: Apr 28, 2021
  2. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    The second issue is due to the web server lacking necessary configuration to serve precompressed gzip files. See https://docs.unity3d.com/Manual/webgl-deploying.html . Unfortunately the error message is awkward, we only realized to improve the error message to more human readable form into Unity 2021.1 and newer.

    If you fix that compression configuration, you'll likely then run into the first issue (the compression option is not expected to affect the first issue). The memory issue looks a bit odd, it is as if the memory is limited to 256MB. Unity projects should all be using "Wasm memory growth" feature, which should get close to 2GB of memory in Unity WebGL builds on desktop browsers. I wonder if that is maybe not enabled for your build. Does the memory error occur on all browsers? Any chance you'd have a live site hosted somewhere to check out?

    You can try overriding the memory size using an Editor script, for example the attached buildWebGL.cs. See the usage instructions in the comments at the top that file.

    There is a line like "PlayerSettings.WebGL.memorySize = 256;" in the file. Change that to e.g. 512, 768 or 1024 and try redoing the build. I am curious whether that might affect the issue. Note that more memory is not necessarily always better here, since the higher that number is, the more wasm memory will be available, but the less will then be left for the JavaScript VM to use, so there is a balance. My guess is based on that log that your project was capping out at around 256MB, but it should automatically grow as needed.
     

    Attached Files:

  3. jonkuze

    jonkuze

    Joined:
    Aug 19, 2012
    Posts:
    1,709
    Thank you for the quick response. I will try to re-configure my web server. In my case, I am using Node.js / Express to test my WebGL Builds locally. Later when I am ready, I plan to use Firebase Hosting or Netlify to serve the static files. So I guess I shouldn't have issues then with these hosted services. I would imagine Firebase Hosting or Netlify enable compression by default on their end.

    I found this article on Compression with Node.js. It seems it has the steps I need to configure it properly.
    https://medium.com/@victor.valencia.rico/gzip-compression-with-node-js-cc3ed74196f9

    Maybe Unity should add some Node.js code examples as well to the documents? As currently it only has info for IIS and Apache.

    P.S. regarding the memory issue, I'm only testing on latest Chrome browser version. I guess i'll give Firefox or Edge a try to see if this is only a Chrome bug issue.

    Thanks again, i'll report back with any updates.