• 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 /
  • Help Room /
avatar image
0
Question by billpoole · Sep 27, 2017 at 08:47 AM · meshmemory leak

Cannot destroy a Mesh after it has been displayed in a frame?

I'm building an application that creates a terrain as a set of tile game objects, where each tile has a dedicated mesh. But I'm finding that when I destroy a tile game object (and its corresponding mesh) after that tile has been visible in a frame, the Windows build leaks memory. There is no leak while running in the Unity Editor however.

In a Windows build, if I create and destroy the tile game object and corresponding mesh in a single frame before the tile is displayed, there is no leak. If the tile is created outside of the view of the camera and then destroyed (while still out of view) then there is no leak. If the mesh renderer is not enabled, then the tile can be created and destroyed with no leak.

Basically as soon as there is one frame where the tile is visible, destroying the tile/mesh becomes ineffective (i.e. there is a memory leak).

Below is some code to demonstrate the issue:

 IEnumerable TestMemoryLeak(Tile tile)
 {
     Mesh mesh = null;
     while (true)
     {
         if (mesh == null)
         {
             // Mesh that is created once and reused
             mesh = CreateMesh(tile); 
         }
         
         // Mesh that is created each loop
         var mesh2 = CreateMesh(tile); 
 
         // Create the tile game object
         var tileGameObject = Instantiate(TilePrefab, Vector3.zero, Quaternion.identity);
         
         // Set the mesh on the tile game object.  Setting to "mesh" instead of "mesh2"
         // results in no leak.  i.e. reusing an existing mesh is okay (no leak) - as long 
         // we don't update that mesh's vertices/triangles.  If we do, it leaks.
         var meshFilter = tileGameObject.GetComponent<MeshFilter>();
         meshFilter.mesh = mesh2;
 
         // Make the mesh visible.  Commenting this out results in no leak because the 
         // tile isn't ever actually displayed.
         tileGameObject.GetComponent<MeshRenderer>().enabled = true; 
         
         // Yield a frame so the mesh is actually displayed.  Commenting this out 
         // results in no leak because the tile is destroyed before it is displayed.
         yield return null; 
         
         // Destroy the tile game object and the per-loop mesh
         Destroy(tileGameObject);
         Destroy(mesh2);
         
         yield return null;
     }
 }

I'm very much hoping someone can help me out. Many thanks in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by billpoole · Sep 29, 2017 at 04:59 AM

Okay - even more simply, I'm finding that I don't even need to create/destroy tiles/meshes to cause this memory leak. If I take an existing tile that has already been displayed in a frame, then update its mesh's vertices/triangles on each subsequent frame, the memory leaks each frame.

If I move the camera so the tile object can no longer be seen, the memory leak stops. Move the tile back into view, and the memory leak restarts.

Again, this doesn't happen when running in the Unity Editor - only when I'm running in a Windows build. I haven't tried other platforms yet, so it may leak in builds for other platforms as well.

My guess is there's an issue around the mesh renderer because the leak only happens once a mesh has been displayed in a frame. If the camera is moved during execution such that the mesh being updated each frame is moved out of view of the camera, the leak immediately stops.

Code to illustrate:

 IEnumerator TestMemoryLeak(GameObject tileGameObject)
 {
     // Create a random one-triangle mesh
     var tile = tileGameObject.GetComponent<Tile>();
     Mesh mesh = CreateRandomMesh(tile);
     tileGameObject.GetComponent<MeshFilter>().mesh = mesh;
     
     while (true)
     {
         // Updates the existing mesh to another random one-triangle geometry
         UpdateRandomMesh(mesh, tile);
         yield return null;
     }
 }
 
 private Mesh CreateRandomMesh(Tile tile)
 {
     var mesh = new Mesh();
     UpdateRandomMesh(mesh, tile);
     return mesh;
 }
 
 private void UpdateRandomMesh(Mesh mesh, Tile tile)
 {
     var vertices = new Vector3[3];
     for (var i = 0; i < 3; i++)
     {
         vertices[i] = new Vector3(UnityEngine.Random.Range(tile.Bounds.Left, tile.Bounds.Right), 
             tile.MaxHeight,
             UnityEngine.Random.Range(tile.Bounds.Bottom, tile.Bounds.Top));
     }
 
     mesh.vertices = vertices;
     mesh.triangles = new int[] { 0, 1, 2 };
 
     mesh.RecalculateBounds();
     mesh.RecalculateNormals();
 }


Comment
Add comment · Show 1 · 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 billpoole · Oct 04, 2017 at 06:39 AM 0
Share

After further investigation I found that the memory leak occurs when using the Direct3D12 graphics API for a Windows build. The leak does not occur when using the Direct3D11 graphics API.

Therefore, I conclude that there is a defect in the Direct3D12 graphics API where dynamically creating, rendering then destroying procedural meshes causes a memory leak as soon as the meshes are rendered.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

121 People are following this question.

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

Related Questions

Memory usage increase when voxels deleted?! 0 Answers

How to remove not saved memory - meshes? 0 Answers

MeshCollider Convex Mesh Descriptor Is Invalid 1 Answer

Decals on deforming 2d mesh how? 0 Answers

How to implement dart game? 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