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();
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 ?

So, despite me testing this for shits and giggles, I do wonder why you would want to instantiate over 2000 (in this case even 700k!) at once, I literally don’t see why you would ever want this to happen.

Apart from a long time waiting to finish the loop, the memory will of course go up (in my case it was somewhere around 3 to 4 GB), because you create instances of something, and these instances need to places somewhere to be remembered; And the RAM is responsible to do exactly that. I see no way of minimizing the memory usage, doing exactly what you try to do.

Generally, if you feel the need to have so many GOs created at once, then you might want to reconsider what you are doing. Not only is this going to junk up the memory, but also may put a considerable stress on the rendering part (assuming you have a blank new scene with that little script in it, nothing else done) (as I experienced a dramatic FPS-loss).
There are many ways to optimize the procedural generation of GOs, for example doing them in batches, or only have a certain amount of them created around the player. Admittedly I have never really played around with that kind of stuff, but that’s conceptually how I would approach that.

By the way, I also wonder why created a dictionary containing these 700k Objects, this is also going to junk up memory big time…