# How can i create 700,000 cubes, memory usage get to 91% and above it?

var key = thisKv.Key;
foreach (var thisValue in thisKv.Value)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.parent = emptygameobject.transform;
var renderer = cube.GetComponent<MeshRenderer>();
renderer.material = textMaterial;
cube.transform.position = new Vector3(-thisValue.Y, 0, thisValue.X);
}


thisKv.Value Value contain over 700000 And then when it's creating the cubes:

GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);


And make the loop very fast my memory usage of the pc get to 91% and more and i need to shut down the unity editor.

Is there any other way to handle so much cubes ?

• Question 1) Why do you need so many cubes? What game function do they serve? There might be more efficient ways to serve that function. Question 2) Why a key-value structure for something so huge that you appear to be iterating non-randomly anyway? – DMGregory Oct 7 '17 at 0:51
• You're trying to make a voxel landscape (like Minecraft) but you're trying to treat each block as its own entity, which is not needed. Blocks should just be a compact data structure from which you build a surface mesh. – Draco18s Oct 7 '17 at 3:55
• There is a reason Minecraft doesn't use a stock game engine. The game is doing something very unusual. Stock game engines aren't optimized for that. – Philipp Nov 12 '17 at 11:21