4
\$\begingroup\$

I've got about 120mb of vertex and indices data, but when I set the data in the vertex and indices buffer it ramps up to about 300mb of memory used, this isn't really acceptable and I don't know why it is, my code to set them is:

        vertex_buffer = new VertexBuffer(device, VertexPositionColor.VertexDeclaration, chunk_vertices.Count, BufferUsage.WriteOnly);
        vertex_buffer.SetData(chunk_vertices.ToArray());
        index_buffer = new IndexBuffer(device, IndexElementSize.ThirtyTwoBits, chunk_indices.Count, BufferUsage.WriteOnly);
        index_buffer.SetData(chunk_indices.ToArray());
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Well, from your code snippet there, be aware that List<T>.ToArray() actually copies all the elements to a new array, so obviously you end up with twice the amount of data in memory. Then, I'm not sure but I think SetData might also make its own copy, making it three times bigger. Do you think that might be the reason? \$\endgroup\$ Commented Mar 3, 2012 at 1:55

1 Answer 1

1
\$\begingroup\$

You are storing the vertices and indexes in the chunk_ variables to start with, which have what I'll call X amount of memory.

You then call new VertexBuffer() and new IndexBuffer() with a count and data types. These new() calls have to allocate memory to hold count*sizeof(data type) that will be stored in the buffer plus have some overhead for their own class members. So you now have at least 2X.

Further, the calls to ToArray() as noted by David in the comments also make copies of this data, so we are now looking at 3X.

We can probably assume that your project consists of more than just this one set of data and these calls so the original value of X should be something close to half the amount of change between 120 and 300 (since we copy it twice, one copy would be about the size of X) so the original data size would be something like (300 - 120) / 2 or 90 MB.

If we split this between your Vertex and Index buffers, and noting that your vertices are 16 bytes each and your indexes are 4 bytes each (from your code above) we can figure that your original data would be something like 72 MB of Vertex data and 18 MB of Index data with 4 million+ vertex and index records. This seems a bit high to me but who knows what you are rendering.

If these seem like the correct numbers, then this explains it. If not, then you are looking at a memory leak somewhere else in your application and may need to do a little more advanced digging.

If you really want to dig into this I would suggest a tool like ANTS memory profiler. They have a free 2 week trial to get you started.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .