Search Unity

Webgl Garbage Collection

Discussion in 'Web' started by unity_7E5C5A8370E649B26E44, Mar 21, 2024.

  1. unity_7E5C5A8370E649B26E44

    unity_7E5C5A8370E649B26E44

    Joined:
    Mar 21, 2024
    Posts:
    3
    Hello
    I noticed that webgl isnt clearing memory so i did a small test, i created a new empty project, added one script to create a big byte array, one button to remove the reference, one button to do garbage collection
    What happened was that when i clear the reference the already created bytes arent being cleared, and calling GC.Collect isnt working as well since some threads that that calling it in webgl wont do effect since it only clears memory on the start of the frame
    So how do we clear that memory? Lets say we're unzipping a file or doing texture operations that uses byte arrays how do we clear that memory after that?
     

    Attached Files:

  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,013
    What editor version? Is it a development build? If not, try a normal or release build.

    Could you post your test code? Perhaps there's a simple oversight, such as assigning the byte array to a field so it won't be garbage collected.
     
  3. unity_7E5C5A8370E649B26E44

    unity_7E5C5A8370E649B26E44

    Joined:
    Mar 21, 2024
    Posts:
    3
    We tested it on 2020.3.36, and on 2022.3.7f1

    Here is the test code:

    public class MemoryClearingTest : MonoBehaviour
    {
    public int size;
    public byte[] byteArray = null;

    public void OnSizeChanged(string newSize)
    {
    try
    {
    size = int.Parse(newSize);
    }
    catch (Exception e)
    {
    Debug.Log(e);
    }
    }

    public void CreateBytes()
    {
    if (size == 0)
    {
    size = 20;
    }
    byteArray = new byte[size * 1024 * 1024];
    for (int i = 0; i < byteArray.Length; i++)
    {
    byteArray[I] = (byte)(i % 256);
    }
    }

    public void ClearReference()
    {
    byteArray = null;
    }

    public void GarbageCollect()
    {
    GC.Collect();
    }

    }[/I]


    What im doing is creating a new byte array and then releasing the reference but it remains in the memory and if you keep creating it it'll crash in the end
    Saw documentations that you should try to add buffers and reuse memory, but since its an old version alot of the file related functions were still not supporting NativeArray and so it uses alot of byte arrays
     
    Last edited: Mar 22, 2024
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,013
    Does this give you the expected behaviour in the editor or a desktop build?
     
  5. unity_7E5C5A8370E649B26E44

    unity_7E5C5A8370E649B26E44

    Joined:
    Mar 21, 2024
    Posts:
    3
    Only in the Webgl Build, i saw online that Webgl clears memory between frames and when you reload scenes, but after testing it didnt seem to be clearing any memory
    In the test memory keeps growing until it says that its out of memory when it reaches the 2 gb threshold