Archived

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

writing your own memory manager

This topic is 5314 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

What are the pros and cons of writing your own memory manager? A pro to me would be that it would be faster than using the default new/delete which just call malloc/free anyway. Related to this, is there a way to make STL objects use your memory allocation, or are you stuck with them using global new/delete? I know you can overwrite the global new/delete, but I read that this is a bad idea. Is there a way to not overload the global new/delete and still have STL use your own memory allocation? Is it even worth writing your own memory manager, or do commercial games ship with code that uses default new/delete?

Share this post


Link to post
Share on other sites
All STL containers take as a template argument type an allocator. The standard library allocator uses the default new/delete, but you can write your own allocator, as long as it conforms to the interface of STL allocators.

Check out this page for more information on allocators.

Share this post


Link to post
Share on other sites
quote:
Original post by foofightr
What are the pros and cons of writing your own memory manager? A pro to me would be that it would be faster than using the default new/delete which just call malloc/free anyway.


Pros:
- You''d learn a lot
- You could make it work the way you wanted (ie. add functionality like auto-defrag, logging, built-in overrun checking, whatever)
- You *maybe* could make it faster, but I doubt it. I''ve already done some testing on new/delete and malloc/free, and they''re pretty darn fast already.

Cons:
- a lot of work
- re-inventing the wheel
- complicated, prone to obscure bugs

Share this post


Link to post
Share on other sites
I don''t know about new and delete, but I am sure that malloc relies on the OS functions that allocate memory. So, I higly doubt you can beat the OS with your own C implementation.
Also, malloc allocates memory in a friendly way for the OS, if you make your own memory management thing, you have to virtually create your own OS.

Height Map Editor | Eternal Lands | Fast User Directory

Share this post


Link to post
Share on other sites
Well, of course you can''t come close to beating the provided low-level management functions at what they do, but that''s not the reason for writing your own memory manager in the first place. Most commonly, you allocate a large chunk of memory using the low-level functions, then partition it to your game''s systems as necessary. With a little bit of work, you can get a pretty nice system working that runs much faster than having to allocate chunks from the heap every time. Sometimes such a system is even necessary, as when you''re allocating an AGP chunk and you have to divide it amongst various custom vertex buffers.

My stance on this is that you probably shouldn''t worry about this until it''s necessary, unless it''s something that interests you. For me, it became necessary, and the process of constructing it was both fun and educational.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
one other pro:

is that if u really understand how ur system works u can get 40% to 65% more speed than new implementation that''s with vs.net(i tried my self) (hint:check siggraph for memory allocation and management)

Share this post


Link to post
Share on other sites