Search Unity

Camera.Render() Memory consumption

Discussion in 'Scripting' started by kogen, Jan 31, 2019.

  1. kogen

    kogen

    Joined:
    Dec 2, 2015
    Posts:
    6
    Dear Unity-Crew,

    I have tracked a weird behavior in my application.
    When I click a button, I render a texture using this function (this is a stripped down version, that does not save the rendered screenshot, but I have only posted the important part here):

    Code (CSharp):
    1.         IEnumerator takeScreenShot(ScreenShotEncodingType screenshotType, string postfix = "", bool automatic = false)
    2.         {          
    3.             Rect cropArea = CanvasUIManager.Instance.GetCropRect();
    4.             Vector2 renderSize = CanvasUIManager.Instance.GetRenderSize();
    5.             float scaleFactor = renderSize.x / cropArea.width;
    6.  
    7.             RenderTexture rt = RenderTexture.GetTemporary((int)Mathf.Ceil(Screen.width * scaleFactor), (int)Mathf.Ceil(Screen.height * scaleFactor), 24);
    8.  
    9.             Camera.main.targetTexture = rt;
    10.             Camera.main.Render();
    11.             Camera.main.targetTexture = null;
    12.  
    13.             RenderTexture.ReleaseTemporary(rt);
    14.             RenderTexture.Destroy(rt);
    15.             System.GC.Collect();
    16.  
    17.             this.CorrScreenshot = null;
    18.  
    19.             yield return new WaitForEndOfFrame();
    20.  
    21.         }
    The problem is, that somehow the RenderTexture does not seem to be removed from memory completely.
    The Application starts, then I click on the button, then a huge chunk of memory is allocated and released after the rendering but around 1/3 of the newly allocated memory is not freed up again.
    When I comment the line that assigns the render texture to the camera targettexture nothing is created or allocated as I expected so it has to be the part when the render texture gets assigned to the camera and the camera finally renders into this texture.

    I can't understand this behavior especially cause every example I could find uses Render Texture in this way. I even added the GC Collect without any success.

    upload_2019-1-31_21-13-26.png
     
    Last edited: Feb 5, 2019