Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Excessive memory consumption when loading images and unable to free the memory

Discussion in 'Web' started by Breadline, Dec 30, 2019.

  1. Breadline

    Breadline

    Joined:
    Jan 16, 2018
    Posts:
    2
    Hi!

    I have a simple script that displays images on screen. It works well on PC, but on mobile I keep running out of memory after just a few images. The images are less than 10MB's of size, but loading one will use at least 100MB of memory. And I am unable to free that memory no matter what. I've read dozens of forum posts and some articles about freeing memory in webgl and I haven't found anything that helped me.

    Is there a way to display images with less memory consumption? I've tried few ways, all with pretty similar results. This is how I've most tried doing it:

    Code (CSharp):
    1.                 using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url))
    2.                 {
    3.                     yield return uwr.SendWebRequest();
    4.                     if (uwr.isNetworkError)
    5.                     {
    6.                         Debug.Log(uwr.error);
    7.                     }
    8.                     else
    9.                     {
    10.                         sphereImage = DownloadHandlerTexture.GetContent(uwr);
    11.                         sphere.GetComponent<Renderer>().material.mainTexture = sphereImage;
    12.                     }
    13.                     uwr.downloadHandler.Dispose();
    14.                     uwr.Dispose();
    15.                     Resources.UnloadUnusedAssets();
    16.                 }
    It works well in Unity editor. The memory doesnt stack up. It only stacks on the WebGL build. And it wouldn't even be an issue if the process consumed less memory but mobile devices are done after just 2-3 images. Any tips?
     
  2. bogdan-serbanescu

    bogdan-serbanescu

    Joined:
    Jun 14, 2017
    Posts:
    20
  3. ammar_12435

    ammar_12435

    Joined:
    Jun 28, 2018
    Posts:
    24
    How do you check one image is using at least 100MB of memory whilst loading?
    Also, are building for asm or wasm?
    If asm, what is the heap size you are allocating while building.
     
  4. hexagonius

    hexagonius

    Joined:
    Mar 26, 2013
    Posts:
    98
    I am also looking for the right solution of continuous texture downloading and loading. In my case it would be sufficient to reuse the same texture the Downloadhandler creates. but that would mean writing a DownloadHandlerScript that performs the same worker thread decompression and such and I am not even sure if you can replicate that from managed code. Do you guys know the correct, full process by now?
     
  5. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589
    Under the hood, Unity and emscripten use the <img> dom element to load textures, but it seems that Chrome and Safari has memory leak there.
    If you have to load lots of images, it'll be better to try and use external image decoding lib. Like jpeg-js or compile libjpeg to wasm and use it