Skip to main content
GameDev.net gamedev.net
🔒 Locked

Memory Management- worth it?

Started by okonomiyaki Jan 20, 2005 at 9:16 AM 4 replies 750+ views
Original Post
okonomiyaki
okonomiyaki
I'm designing the system for a decent-sized game, but it's nothing huge. I'm trying to decide whether or not to implement a memory manager, like this one: http://www.gamedev.net/reference/programming/features/enginuity2/page2.asp I know that it can be really beneficial, but it seems like a lot of hassle when I would have to decide when or not to use a smart pointer, and make sure everything gets derived from the basic object (I'd have to make wrappers for basic types such as int?) Now, don't get me wrong, I use custom containers for arrays which internally handle memory. So every array basically handles itself, but I don't have refcounts, so you can't really pass around these arrays. I've never really found a problem with this, as usually it's 3 scenarios: it's a private member of a class so it only uses it internally, it's just a temp array that a function uses, or I do need to pass it and it calls the copy constructor. The copy constructor copies the data over.. but, I never do this with large arrays, so I think it's worth it. I also use the CRT debugging functions, and my program checks for a corrupt heap every frame and also memory leaks every so often. Using these in previous projects, I've been able to keep a good handle on memory. This has got to save some processing power as well. What do you all think? Am I insane, or is *total* memory management something I can do without?
CoffeeMug
CoffeeMug
I found that boost shared pointers are more than sufficient for my needs. It isn't hard to learn how to use them properly, they're well designed, well implemented and well tested, they provide excellent memory management with minimum hassle and almost no performance hit... the list goes on.
snk_kid
snk_kid
Quote:
Original post by okonomiyaki
but it seems like a lot of hassle when I would have to decide when or not to use a smart pointer.


what is hard about it? maybe we can help you make it easier to decide.

Quote:
Original post by okonomiyaki
and make sure everything gets derived from the basic object (I'd have to make wrappers for basic types such as int?)


No, you can have non-intrusive reference counting system by moving the reference counting mechnisim into the smart pointer itself, there for anything can be be reference counted not just classes, the system in the link is known as intrusive reference counting. The real question is why would you ever want to allocate just one integer on the heap?

Quote:
Original post by okonomiyaki
I use custom containers for arrays which internally handle memory.


assuming C++ why? when you can write/use custom allocator types for standard library containers, instead of re-inventing the wheel that will most likely turn out to be a flat tire [wink]

Quote:
Original post by okonomiyaki
What do you all think? Am I insane, or is *total* memory management something I can do without?


You don't need total memory management but if you want any kind of decent performance you need to help the generic memory manager in some way.

For example allocating alots of small objects at random/arbitrary periods of time will hinder your preformance, you may end fragmenting the heap, so you its worth spending the time on pooling them.

In general allocating large chunks of unintialized memory of a known object size at app intialization time and just initializing it when needed and re-using that memory is much better than dynamically allocating bits of memory and initializing it at the same time then de-initializing and de-allocating it here and there.

You don't have to write this stuff your self, the boost library have a package of pooled allocators (including an STL compliant pooled allocator type), and a a great familly of smart pointers for different situations.

[Edited by - snk_kid on January 20, 2005 10:48:00 AM]
okonomiyaki
okonomiyaki
Quote:
Original post by CoffeeMug
I found that boost shared pointers are more than sufficient for my needs.


Yeah, I just looked at those, and they don't look bad. I've been kind of hesitant to add libraries to my engine, as last time I ended up poorly managing them and it became a tangle of different libraries. I don't really want to add many libraries to my engine, especially when I don't know them at all. At least I know my code and it's easier to debug :D

Yeah, I'm not writing everything from scratch though. I have objects that I have written before and used in tons of projects. Like my custom containers. I like being able to customize them... But, for something like smart pointers, when I would have to write it from scratch, maybe a library is a good idea.

Quote:

what is hard about it? maybe we can help you make it easier to decide.


It's not that it would be difficult, but every time I declare a pointer I'd have to take a second to decide whether or not to use a smart pointer. Gah.. if it's worth it.

Quote:

The real question is why would you ever want to allocate just one integer on the heap?


I wouldn't, but it was an example that helped my case :P I looked at the boost smart pointers, and yeah, they use refcounts within the pointer itself.. wouldn't this increase the size of the pointer too? Bah, maybe I should just try it and see the benefits myself!

Quote:

assuming C++ why? when you can write/use custom allocator types for standard library containers, instead of re-inventing the wheel that will most likely turn out to be a flat tire


Because, like I said before, I've written my own ones for the fun of it, and have ended up using them in many projects and have liked them very much. I didn't like STL the last time I tried it, but I was pretty inexperienced then, maybe I should try it again. Most STL code just seems more cryptic to me than it should be (worst example- a tree using std::map)


Yeah, I've always kept pooling something todo in the back of my mind. Never have been sure how to implement it though. I'll keep looking at boost to see what kind of pointer and pooling help it offers!
snk_kid
snk_kid
Quote:
Original post by okonomiyaki
Quote:

what is hard about it? maybe we can help you make it easier to decide.


It's not that it would be difficult, but every time I declare a pointer I'd have to take a second to decide whether or not to use a smart pointer. Gah.. if it's worth it.


Well it really depends on the situation, in all my recent code i have virtually no use of raw pointers, i stick to references & smarts pointers strickly, i only ever touch raw pointers if i need to make some kind of efficent data structure even then, so its only internal those raw pointers will never see the living day outside of internal use, if i need to expose the internals of data structure with-out breaking encapsulation i'll do it via some form of iterator/vistor.

The thing i like about boost's smart pointers they have different type of smarts pointers for very specific situations (then again you can get policy based smart pointers you can configure for those situations but i just like having different type of smart pointers myself).

If i just want to implement RAII techniques i don't need to use any form of reference counting boost have a smart pointer for that boost::scoped_ptr/array one for a single object & one for raw arrays (then again so does the standard library std::auto_ptr but can't be used for raw arrays).

if i'm working with objects with embedded reference count that require explicit de/increment of reference count and release (like in the enginuity series or COM objects) i can use boost::intrusive_ptr, again i don't need to use a reference counted smart pointer for that.

Quote:
Original post by okonomiyaki
Quote:

The real question is why would you ever want to allocate just one integer on the heap?


I wouldn't, but it was an example that helped my case :P


just kidding, you may want to share a dynamically allocated raw array of built-in type then it would be acceptable, i wouldn't bother writing wrappers for built-in primitives just for sake of getting them reference counted, non-intrusive reference-counted smart pointer all the way here.

Quote:
Original post by okonomiyaki
I looked at the boost smart pointers, and yeah, they use refcounts within the pointer itself.. wouldn't this increase the size of the pointer too? Bah, maybe I should just try it and see the benefits myself!


All of them are not reference counted only shared_ptr/array, besides the extra bit of weight is minute really in terms of what you gain in return its negligible and they are almost as efficent as a raw pointer, plus you can reap the benfits of *weak references* via boost::weak_ptr from a shared_ptr/array.

Quote:
Original post by okonomiyaki
Because, like I said before, I've written my own ones for the fun of it, and have ended up using them in many projects and have liked them very much. I didn't like STL the last time I tried it, but I was pretty inexperienced then, maybe I should try it again. Most STL code just seems more cryptic to me than it should be (worst example- a tree using std::map)


This is the thing about STL, if you come from a back ground of using traditional OO collections framework, STL does require a little adjustement of the mind-set.

Maybe you just need some good resources on STL & modern C++ in general (advance stuff on templates, stuff your mother never told you about [smile]), if you do a search of the forums you'll find people posting lots of links to good mordern C++ books & online resources.

Another library apart from boost worth looking into is the loki library.

[Edited by - snk_kid on January 20, 2005 1:53:27 PM]
okonomiyaki
okonomiyaki
Quote:

The thing i like about boost's smart pointers they have different type of smarts pointers for very specific situations (then again you can get policy based smart pointers you can configure for those situations but i just like having different type of smart pointers myself).


Yeah, and there's a whole learning process involved there. I don't want to get bogged down learning all kinds of different libraries, I want to get something done even if it's not done the best way. But perhaps learning STL and smart pointers is beneficial enough that I should sit down and do it.

Quote:

Maybe you just need some good resources on STL & modern C++ in general.


Yeah, I'm looking at some right now (well, not modern C++ but STL). I understand C++ enough to understand STL, it's just that overall it can look cryptic. I'm not worried about STL being confusing because of my lack of C++ knowledge (though it may play a part sometimes), I'm worried that it will take me so long to learn everything about STL: the most efficient ways of doing things, how it handles memory, etc. Though I have found some good tutorials (like this one: http://www.cs.brown.edu/people/jak/proglang/cpp/stltut/tut.html#38), there still a lot to be learned about how to really efficiently use it.

But anyway, I suppose it's good for me to learn it anyway so that I can say I know it. Whether I use it or not, well, we will see after I learn it :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.