Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jesta · Sep 06, 2012 at 05:15 PM · guimaterialperformancerendertexturememory

RenderTexture causes memory problems

Hi, I have the following code placed in a GUI Window function. I use this to save a RenderTexture rendered by a camera in the "finalRT" variable.

     public RenderTexture finalRT;
     
     void NodeWindow(int windowID)
     {
         RenderTexture heightmapRT = RenderTexture.GetTemporary(1024, 1024, 24, RenderTextureFormat.ARGB32);
     
         GameObject renderCamera = new GameObject();
         renderCamera.AddComponent<Camera>();
         renderCamera.GetComponent<Camera>().backgroundColor = Color.black;
         renderCamera.GetComponent<Camera>().orthographic = true;
         renderCamera.GetComponent<Camera>().orthographicSize = 5;
         renderCamera.GetComponent<Camera>().transform.localPosition = new Vector3(9999, 10009, 9999);
         renderCamera.GetComponent<Camera>().transform.localRotation = Quaternion.Euler(new Vector3(90.0f, 0.0f, 0.0f));
     
         GameObject renderPlane = GameObject.CreatePrimitive(UnityEngine.PrimitiveType.Plane);
         renderPlane.transform.localPosition = new Vector3(9999, 9999, 9999);
         renderPlane.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, 0));
         renderPlane.GetComponent<MeshRenderer>().sharedMaterial.shader = GetNodeByOutput(finalInput.matchedOutput).material.shader;
         renderPlane.GetComponent<MeshRenderer>().sharedMaterial.CopyPropertiesFromMaterial(GetNodeByOutput(finalInput.matchedOutput).material);
     
         renderCamera.GetComponent<Camera>().targetTexture = heightmapRT;
         renderCamera.GetComponent<Camera>().Render();
         RenderTexture.active = heightmapRT;
                 
         finalRT = heightmapRT;
                 
         renderCamera.GetComponent<Camera>().targetTexture = null;
         RenderTexture.active = null;
         DestroyImmediate(renderPlane);
         DestroyImmediate(renderCamera);
         RenderTexture.ReleaseTemporary(heightmapRT);
     }

The code works and I'm satisfied with the result, but the system memory is constantly going up. I thought I deleted everything generated at the beginning (renderTexture, camera, plane) so I don't understand. If I remove the clean-up code at the end, memory sky-rockets and causes a crash in seconds, so the clean-up does something. It's just not enough (RAM usage goes up 400K/s).

Does anyone have an idea what the problem would be?

Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ScroodgeM · Sep 06, 2012 at 08:20 PM

don't create and destroy objects every call of function. create all needed objects once and use created instances as many times as you need.

btw, GC.Collect() can help in this case, not sure

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by cupsster · Jul 15, 2014 at 01:38 PM

Maybe you can squeeze more performance by caching up this call: renderCamera.AddComponent(); into separate value instead of requesting it in each line again and again. This may cause allocations happen I think. And look into Blitz command as well.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can EditorWindows dynamically display interfaces of other Editor Windows ? 1 Answer

Floating GUI button to change correct material of character 3 Answers

Memory management and C# do's and don'ts? 5 Answers

Memory and Performance 1 Answer

GPU at 15FPS, please help 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges