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

WebGL free managed heap memory

Discussion in 'WebGL' started by eatmicco, Aug 31, 2016.

  1. eatmicco

    eatmicco

    Joined:
    Jan 17, 2013
    Posts:
    2
    Hi guys,

    Is GC.Collect() not working in WebGL?
    I have a case like this :
    Code (CSharp):
    1.  
    2.         Debug.Log(string.Format("TotalMemory #0 : {0}", GC.GetTotalMemory(false)));
    3.         using (var www = new WWW(path))
    4.         {
    5.             yield return www;
    6.  
    7.             var data = www.bytes;
    8.  
    9.             Debug.Log(string.Format("TotalMemory #1 : {0}", GC.GetTotalMemory(false)));
    10.  
    11.             data = null;
    12.  
    13.             GC.Collect();
    14.             GC.WaitForPendingFinalizers();
    15.  
    16.             Debug.Log(string.Format("TotalMemory #2 : {0}", GC.GetTotalMemory(false)));
    17.         }
    18.  
    In editor, TotalMemory #2 certainly less than #1, which indicates the memory being freed. But, in WebGL, the value in TotalMemory #2 is the same as TotalMemory #1 (memory not being freed).

    Can anyone explain why is that? And what do I need to do to free the data in WebGL?
     
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Sorry I missed this question.

    Calling GC.Collect() via scripting has no effect on Unity WebGL. This is because we can't garbage collect when the call stack is not empty. More details about this limitation can be found in the manual.
    At the moment, Unity WebGL will try to perform some garbage collection at the beginning of every frame. Then when a new scene is loaded, a full garbage collection is performed.
     
    benzuk likes this.