• 9
• 10
• 10
• 11
• 17

# Unity My attempts at dealing with memory management of chunk loading - What's a good model approach to do this?

This topic is 888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In my voxel game I am running into an issue in drawing the chunks where there is a lot of memory problems when it comes to building the mesh, so I'm looking for a new design model to base this on. Currently my design model is non-existent and kind of a mishmash/mess.

I'm using Unity 5.x, essentially I've tried this approach so far:

First attempt tried:
Every Chunk GameObject has a meshcreator script on it. However, I've learned this can be a problem when you have 2k+ chunks and leads to wasted memory because that would also be 2k+ meshcreator scripts. That in and of itself isn't really a problem if they aren't doing anything most of the time, but I feel like this leads to less control and more of a mess to have so many scripts like that.

The meshcreator script uses Lists to build Faces, Verticies, UVs, ColliderVerticies, and ColliderFaces. The lists are then converted to an array and applied to the mesh. Presto, the chunk shows up. But the downside with this is that 1) it is using List which in Unity is poorly managed in memory. 2) With each chunk having over 4k of voxels, to be calling List.Add() that many times is very inefficient because you are resizing an array thousands of times and adds garbage collection overhead.

Second attempt tried:
So I went ahead and converted them to arrays instead, predefining a large array size and reusing the array. It has an indexHeight so that every time we add something to the array, the position is increased for the next item to be added. But, as you can guess, when you have 2k+ chunks and each one has several arrays of 65k items, that creates HUGE memory footprint.

 private Vector3[] Verticies = new Vector3[65530];
private int verticiesIndexHeight = 0;
{
Verticies[verticiesIndexHeight] = vertex;
verticiesIndexHeight += 1;
}


Third attempt:

Here is where I'm wondering what the best approach might be. Is it feasable to add a mesh creating script to each chunk? Or would it be better to just have one ChunkMeshCreator script that handles all the work? So instead of having thousands of meshcreator scripts on each chunk, instead a single main script renders them? What have other people done? What might be most efficient for Unity?

Edited by GameObject

##### Share on other sites

But the downside with this is that 1) it is using List which in Unity is poorly managed in memory

This seems like a vague statement. It's been a while since I've used Unity, but is there some reason it is "poorly managed" (whatever that means) in Unity vs "regular" C#?

Second attempt tried:
So I went ahead and converted them to arrays instead, predefining a large array size and reusing the array.

Why didn't you just set the Capacity of List? That way you avoid the resizing issues. Of course, if you know exactly how many you need, then using an array is fine too.

But, as you can guess, when you have 2k+ chunks and each one has several arrays of 65k items, that creates HUGE memory footprint.

It sounds like these arrays/lists are just used temporarily for generating the mesh, then they go away, correct? Then just re-use one (I guess you can attach it to some "world" game object or something, or (eek) make it static, if you know this will only ever be accessed from one thread).