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

Doubled Texture Memory while switching cameras

Discussion in 'Scripting' started by schulze-velmede, Jul 9, 2020.

  1. schulze-velmede

    schulze-velmede

    Joined:
    Jun 19, 2020
    Posts:
    3
    Hi all,

    I want to delete the object laying in the heap, where all rendered informations from a camera are saved at.

    In my app I need to change between two cameras.
    My solution is by simply having only one active at the same time and it's working.

    My problem why I'm texting you is a peak in my memory after switching cameras for about 0.2 seconds.
    The garbagde collector need this time to realize and delete this part in the memory but while it is doing its job,
    the camera I switched to is writing its own rendering into the memory. The result is having the doubled amount of Texture Memory. This peak crashes my app when I try to run it on a mobile device...

    Long story short:
    I want to delete the Texture Memory from the first cam while switching to the second cam instead of waiting for the garbadge collector.

    Does someone know how to find and destroy this data?

    PS: Attached a screenshot from the memory peak occouring while switching cams.
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    You can try System.GC.Collect() and/or Resources.UnloadUnusedAssets() and see if either of those solve the problem.

    If those don't work, a possible workaround might be to have both cameras render to the same RenderTexture (which is then displayed on screen with the UI and a RawImage component).
     
  3. schulze-velmede

    schulze-velmede

    Joined:
    Jun 19, 2020
    Posts:
    3
    Thank you for your advise.
    Both commands (System.GC.Collect() and/or Resources.UnloadUnusedAssets()) didn't do it for me.

    Your idea with a RenderTexture to overwrite the location, where the data is saved to, is a little bit tricky because I'm switching between an orbitcam and a flycam but I'll try it.

    My current workaround is by putting the switching-process into a coroutine and add a new WaitforSeconds(0.1f) after deactivating the previous cam and before activating the next cam. It's dirty and is causing a black cam (no camera is rendering), for 0.1 seconds, which isn't beautiful but it's enough to keep the app alive instead of crashing on some older devices.