Unity memory usage

Hey are there any good tutorials on how to understand the profiler data? I’m creating a voxel type game similar to minecraft and just loading one chunk into the scene.

One chunk is 65,000 blocks and here is my block class.

    public int x, y, z;
    public byte shape;
    public byte material;
    public byte direction;
    public byte lighting;

Here is the memory usage in the profiler.

It says 1.5G is being used. I’m guessing where my chunk memory would be located is in “Other” which is 600MB. When I play the game without building the chunk it goes down to 200MB. Does that sound right?

When I build the game and look in task manager its using exactly 600MB when building a chunk.

So my questions are, how come unity uses so much memory in the editor? Is there a way to figure out what exactly is using memory in the profiler? I know in Godot it shows you how much each texture, etc, is using.

Alrighty I found this package, How to use Unity’s memory profiling tools | Unity, which is pretty darn cool. You can go in and view every single memory allocation you do.

I found out that one of my chunks is only 3MB but the problem was where I was building and combing the mesh faces. Here is an example of how I was building and combing the mesh faces for each block.

        Mesh m1 = new Mesh();
        Vector3[] v1 = new Vector3[4];
        v1[0] = new Vector3(    0.5f,   0.25f,  -0.5f);
        v1[1] = new Vector3(    -0.5f,  0.25f,  -0.5f);
        v1[2] = new Vector3(    -0.5f,  0.25f,  0.5f);
        v1[3] = new Vector3(    0.5f,   0.25f,  0.5f);
        m1.vertices = v1;
        m1.triangles = new int[6] { 0,1,2,0,2,3 };
        Vector3[] uvs1 = new Vector3[] { new(0,0,1), new(0,1,1), new(1,1,1), new(1,0,1) };
        m1.SetUVs(0, uvs1); geometry.Add(m1);

        Mesh m2 = new Mesh();
        Vector3[] v2 = new Vector3[4];
        v2[0] = new Vector3(    0.5f,   -0.25f,  -0.5f);
        v2[1] = new Vector3(    -0.5f,  -0.25f,  -0.5f);
        v2[2] = new Vector3(    -0.5f,  -0.25f,  0.5f);
        v2[3] = new Vector3(    0.5f,   -0.25f,  0.5f);
        m2.vertices = v2;
        m2.triangles = new int[6] { 2,1,0,3,2,0 };
        Vector3[] uvs2 = new Vector3[] { new(0,0,0), new(0,1,0), new(1,1,0), new(1,0,0) };
        m2.SetUVs(0, uvs2); geometry.Add(m2);

        CombineInstance[] combiner = new CombineInstance[geometry.Count];
        for(int i = 0; i < geometry.Count; i++) {
            combiner[i].mesh = geometry[i];
            combiner[i].transform = Matrix4x4.TRS(new Vector3(x, y * 0.5f, z), section.transform.rotation, section.transform.localScale);
        }
        
        m1.name = "Full1";
        m2.name = "Full2";

        Mesh c = new Mesh();
        c.CombineMeshes(combiner, true, true);
        c.name = "CombinedFull";
        section.opaque.Add(new(x,y,z), c);

I was building each face and then using the unity “CombineMeshes” method to pack them all together for the chunk but I needed to make sure to destroy the temporary faces I was building like so.

        GameObject.Destroy(m1);
        GameObject.Destroy(m2);

Oh and if you want an asset to show up in the memory profiler you can give it a name like so.

        m1.name = "Full1";
        m2.name = "Full2";

So you can pinpoint where the problems are.

2 Likes