1

In profiler both device and editor - base line shifted by a diff of 27mb, but image downloaded is of size 700kb and is placed into a RawImage of size 300x300 and format RGB32.

Any idea why there is such a big change in memory?

Here is the code used:

public class randomscript : MonoBehaviour
{
    public RawImage im;


    public void OnClick()
    {
        //SocialPictureCache.LoadImage("https://wallpapercave.com/wp/PPMnOZM.jpg", SocialPictureCache.PictureType.Small, callback);

        StartCoroutine(load("https://wallpapercave.com/wp/PPMnOZM.jpg"));
    }

    public IEnumerator load(string imagePath)
    {
        using (UnityWebRequest request = new UnityWebRequest(imagePath, UnityWebRequest.kHttpVerbGET))
        {
            request.downloadHandler = new DownloadHandlerTexture();

            yield return request.SendWebRequest();

            if (request.isNetworkError || request.isHttpError)
            {
                Debug.LogError("Error while trying to load image from streaming assets - " + imagePath);
                im.gameObject.SetActive(false);
            }
            else
            {
                im.texture = DownloadHandlerTexture.GetContent(request);
            }

            request.Dispose();
        }
    }
}
hexstorm
  • 318
  • 4
  • 14
aderw
  • 41
  • 4
  • Additional info - More calls i make basically is adding up to the mem. 7 such images downloads results in a spike from 240mb to 450Mb mem usage. – aderw Jun 07 '21 at 11:50
  • Just btw: this `request.Dispose();` is redundant .. you are inside a `using` block so when reaching the end of the block the `request` will be disposed anyway ;) – derHugo Jun 07 '21 at 12:04
  • And do you mean `RGBA32` or `RGB24`? ;) – derHugo Jun 07 '21 at 12:04
  • its ARGB32 ```Texture = new Texture2D(4, 4, TextureFormat.ARGB32, false); Texture.LoadImage(bytes);``` – aderw Jun 07 '21 at 12:58
  • i just modifed the code a bit , but results are same – aderw Jun 07 '21 at 12:59
  • Texture = new Texture2D(4, 4, TextureFormat.ARGB32, false); bool success = Texture.LoadImage(response.Bytes); response.bytes are nothing but result of unitywebrequest – aderw Jun 07 '21 at 12:59
  • 1
    in general the memory needed for `Texture` will **always** be bigger then the source image file since a Texture holds additional information and the pixel data inside c# is most probably bigger then the raw bytes! Especially for JPG which is a lossy image compression format ;) – derHugo Jun 07 '21 at 14:04
  • Btw why not in general directly use [`UnityWebRequestTexture.GetTexture`](https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequestTexture.GetTexture.html)? – derHugo Jun 07 '21 at 14:06
  • tried with GetTexture as well - same result – aderw Jun 11 '21 at 08:31

0 Answers0