0

I had some problem when run Webgl on Chrome:

  1. Memory still on ram when I close my Webgl game:

    • Before run my game: 1.00G ram
    • While run my game: 1.9G ram
    • After close my game: 1.8G ram
    • Run game again: 2.5G ram => Array buffer allocation failed
  2. Memory fragmented while refresh page many time => not enough memory for consecutive block (heap) => Array buffer allocation failed

How can I fresh heap after close my WebGL game?

Any suggestion to fix that issues?

gman
  • 100,619
  • 31
  • 269
  • 393

2 Answers2

0

This may not be Unity's fault. Usually when tabs are closed in Chrome, it takes time for the memory to the freed. Sometimes 4 to 10 minutes. This is what I noticed from the Task Manger. It doesn't free up memory right away. Sometimes, you even have to close the current Chrome browser for closed tab memory to be freed. I can't tell if this is a bug or feature. Maybe, Calling GC.Collect() might help.

You should put the code below in your script to see if that would help.

void OnDisable()
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
0

According to Unity Forums In Unity 2019.1, there is proper method to Quit and cleanup the WebGL player:

  • C#: call Application.Quit()
  • JS: call unityInstance.Quit(callback)

You can use JS asynchrous function like this:

unityInstance.Quit(function() {
    console.log("done!");
});
unityInstance = null;

If you are using Application.Quit of C# then you can provide its callback in JS like this:

UnityLoader.instantiate("unityContainer", "%UNITY_WEBGL_BUILD_URL%", {onProgress: UnityProgress,
    Module:{
      onQuit : function(){
        console.log("unity has quit");
      }
    }
});

My Observation/Caution: According to my current observation, it is not freeing the memory and the second is that currently unity 2019 is in beta stage. If anyone use this quit feature please let me know that it is working or not.

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186