Unload Unity gameInstance and release heap memory

What is the correct ways to Unload\Release Unity gameInstance in WebGL build?

We have a SPA (Angular4) with Unity in a tab (Component).

We create the game Instance as following:
var gameInstance = UnityLoader.instantiate(“gameContainer”, “Build/WebGL.json”);

When we are switching tabs,Unity instance does not get destroyed and consumes a lot of memory.
We tried the following and it did not help:

  • Application.Quit()
  • Removing element node var elem =getElementById(‘gameContainer’);elem.parentNode.removeChild(elem)
  • Garbage Collection
  • gameInstance = null
  • Removed all listeners on the document

Any suggestion?

1 Like

Did you find a solution to this? I’m not using Angular, but I’m having a similar problem when I remove the element from the DOM

me too

I’m integrating unity webgl inside an AngularJS app and to clear the memory I simply reload the page when exiting a tab running Unity. I know I loose the advantage of the “one page” but this also allows me to run different Unity instances inside the web app.

In Angular I add a listener on routeChangeSuccess (Which is fired when changing tab) at the beginning of my component controller :

[...]
$rootScope.$on('$routeChangeSuccess', function(angularEvent, current, previous){
            //Avoids reloading when staying on the same route
            if(current.$route.originalPath != previous.$route.originalPath){ 
              $window.location.reload();
            }

});
[...]

I guess there is something like this too in Angular.

1 Like

Not quite. This is the problem with Single Page Applications - they do not reload window. In our case reload will take a user back to login page.
We need a solution without page reloading or reloading inside an IFrame.

At the moment the only way to unload the gameInstance and release memory is by using an iframe, then releasing all references to the iframe when you need it to be unloaded.
It should be possible to do it without using an iframe but this is not working properly at the moment. It’s something we need to fix. Here is the public bug tracker to a related problem.

I’ll stop looking for a workaround then - although my previous PWA app used a router so that I was able to do a page re-load without redirecting to the homepage.

FYI: we are investigating this problem.