Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
3
Question by JasonBricco · Sep 25, 2015 at 12:50 PM · c#meshgraphicsmemory-leakdrawmesh

Graphics.DrawMesh Leaking Mesh Memory

I'm using Graphics.DrawMesh to draw meshes for my terrain chunks (voxel engine). Any time I draw new meshes with DrawMesh, the memory taken up by meshes goes up and it never goes back down again.

Here's the relevant code that I am using:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class ChunkRenderer 
 {
     private static MeshData data = new MeshData();
     public static bool buildingMesh = false;
 
     public Chunk chunk;
 
     public bool updateMesh;
     private bool meshReady;
 
     public Mesh[] meshes = new Mesh[MeshData.MeshCount];
 
     private Vector3 worldPos;
 
     public ChunkRenderer(Chunk chunk)
     {
         this.chunk = chunk;
         worldPos = chunk.GetWorldPosition();
 
         for (int i = 0; i < MeshData.MeshCount; i++)
             meshes[i] = new Mesh();
     }
 
     public void Update()
     {
         if (updateMesh)
         {
             if (!chunk.NeighborsLoaded(true))
                 return;
             
             updateMesh = false;
             ThreadManager.QueueMesh(this);
         }
 
         if (meshReady)
         {
             for (int i = 0; i < MeshData.MeshCount; i++)
                 data.GetMesh(meshes[i], i);
 
             data.Clear();
             meshReady = false;
 
             buildingMesh = false;
         }
 
         for (int i = 0; i < meshes.Length; i++)
             Graphics.DrawMesh(meshes[i], worldPos, Quaternion.identity, Engine.GetMaterial(i), 0);
     }
 
     public void ClearMeshes()
     {
         for (int i = 0; i < meshes.Length; i++)
             meshes[i].Clear();
     }
     
     public void BuildMesh()
     {    
         buildingMesh = true;
 
         for (int x = 0; x < Chunk.Size; x++)
         {
             for (int y = 0; y < Engine.Height; y++) 
             {
                 for (int z = 0; z < Chunk.Size; z++) 
                 {
                     ushort block = chunk.GetBlock(x, y, z);
                     
                     if (block != 0)
                         block.Build(chunk, x, y, z, data);
                 }
             }
         }
 
         meshReady = true;
     }
 }

I assign empty meshes on start. There's a mesh for each material that can be used.

The update function is triggered by an event (this isn't a MonoBehaviour), and when it is flagged for updating, it sends a background thread to go through the voxels and build the mesh data.

That process adds the vertex/triangle/color/normals/uvs to the MeshData object, which stores these. There's only one shared per all chunks because I only assign a single background thread for meshes (the rest handle terrain and lighting).

When the background thread has built the meshes, it sets a boolean which tells the main thread that it's ready since I can't do this on a background thread.

I call the GetMesh function in MeshData to assign the mesh data, and it looks like this:

 public void GetMesh(Mesh mesh, int index) 
 {
     if (vertices[index].Count == 0)
         return;
 
     mesh.Clear();
     mesh.vertices = vertices[index].ToArray();
     mesh.normals = normals[index].ToArray();
     mesh.uv = uv[index].ToArray();
     mesh.colors = colors[index].ToArray();
     mesh.triangles = triangles[index].ToArray();
 }

And then I draw it using Graphics.DrawMesh.

Now, here's the thing:

Any time a chunk is unloaded (too far away from the player), I call ClearMeshes. As you can see, all it does is it calls Mesh.Clear().

I'd expect the memory from the mesh to be freed here, but this is not the case. All the memory from each mesh created stays allocated forever!

I even tried assigning each mesh component (vertices, triangles, etc) to an empty array of length 0 in that function and the same problem happened.

When I commented out Graphics.DrawMesh, but left all the rest, there were no leaks anymore.

Seems Graphics.DrawMesh causes this, but I'm not sure what to do about it.

Any help would be greatly appreciated.

(I've even tried using Destroy() on the meshes and creating new ones, but that didn't seem to do anything to help either.)

Comment
Add comment · Show 1
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 sohussain · Apr 21, 2016 at 09:31 AM 0
Share

Is there any alternative to draw meshes procedurally that does not leak memory?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by elias.ku · Mar 09, 2016 at 11:12 AM

Same problem with iOS / IL2CPP!!! Very critical, please help. Also commenting Graphics.DrawMesh remove memory leaks

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

31 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

Related Questions

Graphics.DrawMeshInstanced Doesn't work with transparent materials 1 Answer

How could I implement a "border" system for my medieval game? 0 Answers

blending between meshes 1 Answer

How to make a Sine wave generated from a unit circle with dynamic frequency and amplitude? 1 Answer

Journey like terrain modification 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