Vertex buffers are unmanaged resources. The garbage collector does not know that they are using a whole bunch of unmanaged memory (and GPU resources) backstage. All he knows is the tiny bit of managed memory that everyone uses.
I talk more about unmanaged resources in XNA in my answer to this question .
You can call Dispose() on each VertexBuffer before it leaks (but after drawing is complete, as it will still be used!) To free up unmanaged resources. This will avoid a memory error, but it will still be very slow!
What you really have to do is create the minimum required vertex buffers only once. The ideal place for this is in your LoadContent function (and then Dispose() in your UnloadContent function). If you have a whole bunch of cubes, all you need is a single vertex buffer that describes the cube that you will use every time you draw a cube.
Obviously, you do not want to draw all your cubes in the same place. This is what the World matrix is ββfor. Each time you draw a cube, set BasicEffect.World to your transformation matrix for that cube and call Apply() .
(The way you install WorldViewProj is fine too. But using a nice API is, well, better.)
If rotation is what you want, use Matrix.CreateFromYawPitchRoll(yaw, pitch, roll) to create your transformation matrix.
More about this, your problem is similar to another question I answered .
(Note: if the vertices themselves really change every frame, you should use DrawUserPrimitives . But note that this is still significantly slower than letting the vertex shader on the GPU handle any transformations.)
Andrew Russell
source share