Search Unity

Often loading and dropping textures from memory

Discussion in 'Web' started by zharko, Jan 18, 2019.

  1. zharko

    zharko

    Joined:
    Jan 25, 2015
    Posts:
    21
    Hi all

    I came across an issue with displaying many textures in webGL build of my game, namely I get out of memory error unpredictably.
    In my game I pull player photos from server through socket connection, decompress them on the client and display them as Texture2D, it all works like charm... almost. From obvious reasons I have to limit the number of photos available on the client side at one given moment, so when new photos are needed to be displayed they are pulled from the server and if too many are present on the client (more than threshold value) I drop the ones that have been used least recently, by dropping I mean resetting all references to it and expect that the GC clears off these textures. But it seems that dropping textures works unpredictably, I get out of memory error unpredictably and I cannot reliably recreate the issue. Interesting thing is that the error occurs even when not many textures have been loaded (50-60 textures 300x300).
    Is there a solid way to manipulate the textures on the graphic card? I understand that once apply method is performed on Texture2D it is loaded onto the graphic card, but is there a way to reliably drop texture from the graphic card?
    Perhaps there are other solutions to my situation? Such as rendering directly from memory not from graphic card - I know this is very slow but maybe would do for my purpose...

    Unity version: 2017.03.0f3, browser: chrome latest as of now.

    I tried on android but never encountered any such issue, until now all works as expected.

    Thanks
     
  2. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    As you've guessed, applying the texture allows you to remove the CPU copy (Texture2D.Apply with 'makeNonReadable' parameter set to true). This is a good start as the RAM essentially has a hard cap. When you're done with your textures, simply destroying the game objects won't be enough - you'll either want to Destroy the texture objects themselves (i.e. Destroy(texture).

    If you find it too difficult to keep track of all the textures you could instead call Resources.UnloadUnusedAssets on a schedule e.g. every 10 seconds. I work on a WebGL application that consists of very intensive run-time texture streaming and cover some basic tips on this in a Melbourne Unite talk (the second half of the video is less sales-pitchy ;))
     
    zharko likes this.