Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unloading Unity3D webgl instance without embedding application or reloading page.

Discussion in 'WebGL' started by kailashvetal47, Nov 27, 2017.

  1. kailashvetal47

    kailashvetal47

    Joined:
    Oct 15, 2014
    Posts:
    7
    Hi

    I am creating WebGLof the unity project and loading the project from html file using UnityLoader.instantiate(). Our game requires more than 1.5 GB space to run so when the game is not running this instance occupying all memory.

    I want to destroy the gameInstance created and free memory from browser completely. So for all forums and post suggest to embed the application or to reload the page.

    Is it possible to do so from a script without using Embed or Page reload?

    Is it possible to have api call to destroy or despose unity instance from the browser memory?
    Something like : UnityLoader.dispose(gameInstance)
     
    Last edited: Dec 21, 2017
    ToddGatts likes this.
  2. savely00

    savely00

    Joined:
    Feb 27, 2014
    Posts:
    36
    We have the same problem.
    Any replies from Unity stuff and /or unity developers are greatly appreciated!
     
    ToddGatts likes this.
  3. anastrophe

    anastrophe

    Joined:
    Feb 8, 2017
    Posts:
    9
    Another - me too.
     
  4. kailashvetal47

    kailashvetal47

    Joined:
    Oct 15, 2014
    Posts:
    7
    anastrophe : Here is the answer from the Unity Support might help you.

    Goal: I want to destroy the gameInstance created and free memory from browser completely.

    Question: Does unity provide any functionality to destroy gameInstance created? example : UnityLoader.dispose( gameInstance )
    Answer: No, Unity doesn't provide any method or functionality to achieve this. Also, there is no way to release memory explicitly in JavaScript, it will be garbage-collected by the GC any moment, but we can't control when this occurs.

    Question: Is there any other way I can destroy Unity game instance completely from Unity engine side.
    Answer:
    Sadly, the answer is no again. You could do:

    gameInstance = null;
    delete gameInstance;

    But the memory won't be released anyway because of the way of JavaScript works. Even, if you try injecting a DOM element with the canvas object and remove it when needed, the memory won't be released. And also, using this approach, you would have to disable a lot of callbacks/listeners and free a lot of arrays, etc. And after that, you would have to create everything by hand again.

    Note: Following options are tried and are not suiting to our needs.
    a. Enclosing Unity instance in iFrame
    b. Reloading Page / Opening another page.

    Why didn't the approach iFrame + reloading (the iFrame only) work for you?

    This link https://stackoverflow.com/questions...nload-a-unity-webgl-canvas-when-changing-page might provide you more details.
     
    savely00 likes this.
  5. anastrophe

    anastrophe

    Joined:
    Feb 8, 2017
    Posts:
    9
    Thanks - really appreciate the follow-up.

    (FWIW my problem with the iFrame and the reloading page option is that I'm building a progressive web app [PWA] and that's not part of the approach/UX)
     
    savely00 likes this.
  6. savely00

    savely00

    Joined:
    Feb 27, 2014
    Posts:
    36
    Same here. We are using Unity build in an SPA app and refreshing a page is not a right solution. Any other suggestions?
     
    Renaud and kailashvetal47 like this.
  7. Renaud

    Renaud

    Joined:
    Nov 6, 2017
    Posts:
    2
    Same thing as @savely00. We are building a single page app with ReactJs, and it would be great to be able to unload the Unity instance properly.
     
    savely00 likes this.
  8. savely00

    savely00

    Joined:
    Feb 27, 2014
    Posts:
    36
    After trying out multiple solution we solved the issue by putting the gameInstance object inside an iFrame. Nothing else worked so far.
     
  9. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    Hi guys,
    its 2019 Feb, do there any new solution available to unload the application in webGL and free the memory?
    If i understand this thread correctly, then there is no way avaialbe as we don't have control over javascript GC?
    Additionally if we use iframe to embed our application and then remove iframe then it will correctly and this is only the way.
     
  10. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    The 2019.1 beta has a "quit" function that should achieve this, but I personally have not yet tested.
     
  11. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    Thank you very much i found this line here:
    "WebGL: Add Quit() function support to the unity instance"
    Hopefully it is same that i am looking for but unity 2019 is still beta format. Although i will check it. If someone have more information about it then please let us know.
     
  12. DaveA_VR

    DaveA_VR

    Joined:
    May 26, 2022
    Posts:
    29
    How about implement a public function that calls Application.Quit() and call that from your Javascript before destroying the container? Worked for me.