• Advertisement

Archived

This topic is now archived and is closed to further replies.

memory allocation habits

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just thought if it''s bad idea to do many small memory allocations, like: Vertex *p = new Vertex; for each vertex, there can be thousands of them. Is the memory consumption larger, does the system need space for each memory allocation? I''m using VC6. --------- I also go to a java course and they there have very very weird habits there. For example have to find if a vertex is isolated in a graph: it just requires a single loop to go through all sides and look at if one is referencing to the vertex. But no! They say it''s better to use function that creates a new list of all sides that reference to the vertex (method offered) and then check if that list size equals to 0. This was just a light example what they teach. But they do this all the time. Allocate memory that they don''t use at all. Are they teaching me bs?

Share this post


Link to post
Share on other sites
Advertisement
Yes, MSVC''s malloc keeps a linked list of memory allocations in debug mode for leak detection. I''m not sure what goes on in release mode, but I''m sure at least one memory model is that there''s a 32-bit "size" word just before the allocated memory.

If these allocations all happen at startup or at one point in the game, this isn''t a big deal--it''s just the "Loading" screen. However, if they''re happening throughout the game, it''s probably that memory allocation is causing a performance hit and you might want to look at doing your own memory management.

Share this post


Link to post
Share on other sites
quote:

I also go to a java course and they there have very very weird habits there.
For example have to find if a vertex is isolated in a graph:
it just requires a single loop to go through all sides and look at if one is referencing to the vertex.
But no! They say it''s better to use function that creates a new list of all sides that reference to the vertex (method offered) and then check if that list size equals to 0.


I''d say as far as good practices go they are teaching you bs, but it might be the case that they want you to familiarise yourself with storage-classes (such as vector) in the course. In that case it''s just a very bad chosen example. It may also be the case that you''ll need the newly created list for something else later on in the program (what do I know?).

In either case, a good rule is to try and organise your data in a way that makes you perform a few big allocations (in the beginning of your program, if possible), rather than many small (especially if they are done in time-critical places in the code). The reason is that allocating memory takes time and by allocating many small chunks (of memory), you not only fragment the memory, but you also consume more memory, since every allocation keeps track of (at least) the start-address and the size of the chunk.

Share this post


Link to post
Share on other sites
quote:
Original post by balloon
I just thought if it''s bad idea to do many small memory allocations, like:

Vertex *p = new Vertex;

for each vertex, there can be thousands of them.

Is the memory consumption larger, does the system need space for each memory allocation? I''m using VC6.


Yes, memory consumption is greater. It''ll use a linked list of allocated memory chunks, each member of which will typically indicate the length of the chunk, and the location of the next chunk (and, of course, the actual memory itself).

Speed-wise, it takes time to find a free chunk, so you should avoid allocating each chunk seperately.

Best is to use new Vertex[SIZE] (the Vertex will need a default constructor for this to work). With this method, you only have to find one large chunk, which reduces both the memory used for housekeeping, and the time taken to find the memory for your data.
quote:

to find if a vertex is isolated in a graph it just requires a single loop to go through all sides and look at if one is referencing to the vertex.
But no! They say it''s better to use function that creates a new list of all sides that reference to the vertex (method offered) and then check if that list size equals to 0.


As amag said, if you want to use the sides later on, it may be better to generate the list because that way you only have to iterate through the sides once. On the other hand, if you aren''t going to use the sides, then it isn''t a good idea.

All your bases belong to us (I know. It''s irony.)

Share this post


Link to post
Share on other sites

  • Advertisement