Search Unity

Camera.Render to render texture massive memory leak 2017.4.2f2

Discussion in 'Scripting' started by madpuppet, Aug 24, 2018.

  1. madpuppet

    madpuppet

    Joined:
    Apr 17, 2012
    Posts:
    15
    I'm doing hundreds of Camera.Render's to renderTexture to calculate visibility information from positions on the map.

    This is causing the editor (or standalones) to reach 30gig of memory and crash - goes up by a gig every few seconds...

    Its just a tight loop of code that moves the camera to different points and directions and takes snapshots which are copied back to memory for processing.


    Code (CSharp):
    1.                                         // render the single mesh using a material override
    2.                                         map.ShowAllButMesh(tx, tz);
    3.                                         camera.Render();
    4.  
    5.                                         // grab copy of single mesh render
    6.                                         RenderTexture.active = m_camera.targetTexture;
    7.                                         renderCopy.ReadPixels(new Rect(0, 0, m_camera.targetTexture.width, m_camera.targetTexture.height), 0, 0);
    8.                                         var meshColor = renderCopy.GetPixels32();
    9.  
    To get around the problem, I've had to put it in as a coroutine and do yield; instead of camera.Render(), which is a bit slower since you waste the remainder of the frame, instead of rendering more images.

    When I reduce to loop count so it doesn't crash, the profiler shows the GFXDriver is hogging 13gigs or more of ram and that is not released until I close the editor completely and reopen it.
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    2,103
    Post the whole method. It's probably because you're not disposing the created texure copy properly.
     
  3. madpuppet

    madpuppet

    Joined:
    Apr 17, 2012
    Posts:
    15
    its leaking memory in that inner loop running that code above over and over. Is there some reason I can't run those 5 lines of code and over and over without losing memory?

    If I remove the "camera.Render" the leak goes away.
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    2,103
    Well, you should try to replace it then with Graphics.Blit or CommandBuffer.Blit.
    Seems like you've already got RenderTexture up an running, shouldn't be much of a problem.
     
unityunity