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

Unload Unity gameInstance and release heap memory

Discussion in 'Web' started by savely00, Mar 29, 2018.

  1. savely00

    savely00

    Joined:
    Feb 27, 2014
    Posts:
    36
    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?
     
    Last edited: Apr 7, 2018
    ToddGatts likes this.
  2. slanden

    slanden

    Joined:
    Nov 9, 2015
    Posts:
    14
    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
     
  3. anastrophe

    anastrophe

    Joined:
    Feb 8, 2017
    Posts:
    9
  4. goddatr

    goddatr

    Joined:
    Mar 16, 2018
    Posts:
    26
    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 :
    Code (JavaScript):
    1.          
    2. [...]
    3. $rootScope.$on('$routeChangeSuccess', function(angularEvent, current, previous){
    4.             //Avoids reloading when staying on the same route
    5.             if(current.$$route.originalPath != previous.$$route.originalPath){
    6.               $window.location.reload();
    7.             }
    8.  
    9. });
    10. [...]
    11.  
    I guess there is something like this too in Angular.
     
    savely00 likes this.
  5. savely00

    savely00

    Joined:
    Feb 27, 2014
    Posts:
    36
    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.
     
  6. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    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.
     
  7. anastrophe

    anastrophe

    Joined:
    Feb 8, 2017
    Posts:
    9
    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.
     
  8. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    FYI: we are investigating this problem.