Search Unity

UnityWebRequest won't release memory after calling dispose

Discussion in 'Asset Bundles' started by scozirge, Nov 23, 2021.

  1. scozirge

    scozirge

    Joined:
    Feb 24, 2015
    Posts:
    80
    I use UnityWebRequest to download a picture( https://graph.facebook.com/4875021559178029/picture?height=256)

    I try running the function 200 times to test UnityWebRequest, see below

    public static void GetSpriteFromUrl(string _url, Action<Sprite> _cb)
    {
    UnityWebRequest request = UnityWebRequest.Get(_url);
    UnityWebRequestAsyncOperation asyncOperation = request.SendWebRequest();
    asyncOperation.completed += (asyncOperation) =>
    {
    /*
    byte[] result = request.downloadHandler.data;
    Sprite s = SpriteConvert.GetSprite(result);
    _cb?.Invoke(s);
    */
    request.downloadHandler.Dispose();
    request.Dispose();
    };
    }

    I comment out the callback part, so while downloading is completed it will call Dispose, and should be no other reference. But it turns out lose huge memory.

    before
    upload_2021-11-23_15-33-18.png


    after
    upload_2021-11-23_15-33-37.png

    I have no idea which part I doing wrong.
    Many thanks
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Your profiler screenshots show texture memory rising, but your code uses UnityWebRequest.Get, not UnityWebRequestTexture.GetTexture, so the downloaded stuff is not placed in the texture memory.
    So, you are missing something here.
     
  3. scozirge

    scozirge

    Joined:
    Feb 24, 2015
    Posts:
    80
    it works! really appreciate!