Sign in to follow this  
GenuineXP

Using Memory Pools

Recommended Posts

Hello. I'm wondering about memory pools and how they're used. How does one use a memory pool in C++? What are the advantages? From what I've read so far (very, very little), memory pools can boost effeciency by setting aside "preallocated" memory for objects. Is this true? Is memory pooling worth the benifits only for large objects or large groups of small objects, or is it also useful for pretty much any object that's used frequently? How is it implemented? Any insight or articles would be great. I'm pretty new to this concept. :-) Thanks!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The benefit, from what I understand, is that you can control the fragmentation of memory so that when you are constantly allocating and deallocating objects during a program it doesn't eventually slow down as it looks for a hole big enough to fit objects into. I'm not a windows kernel expert, but I imagine windows has similar capabilities for defragmenting memory on the heap. Go to The Code Project and do a search. I remember some articles on it in there.

Share this post


Link to post
Share on other sites
The benefit, from what I understand, is that you can control the fragmentation of memory so that when you are constantly allocating and deallocating objects during a program it doesn't eventually slow down as it looks for a hole big enough to fit objects into. I'm not a windows kernel expert, but I imagine windows has similar capabilities for defragmenting memory on the heap. Go to The Code Project and do a search. I remember some articles on it in there.

Share this post


Link to post
Share on other sites
The idea of memory pools, is to have a pool of unused objects (of one size). This starts out empty (one could pre-allocate some objects), when someone needs an object they check this pool to see if there are any. If there are any then they remove one from the pool and use that, but remember to call the constructor on it so it won't have its old state. If there aren't any just allocate it with new. Now when you destroy your object, instead of calling delete on the object, you just add it to the pool of unused object (and call the destructor, to release any internally used resources). This makes sure that we won't have to do more memory allocations than necessary, but it might also means that we keep lots of unused memory in the pool. There are of course lots of optimizations, for example if we have one pool with objects of size N, and another pool with objects of size N/M, and we have too many objects of size N, but need to allocate objects of size N/M. Then we can just remove a couple of objects from the pool of size N, and add M objects to the pool of size N/M. Another optimization could be to allocate more than one object at once, so when we see that we need a new allocation, we might actually make 10, because it's much cheaper, than 10 seperate allocations. Lots of other optimizations exist, a really advanced system might even try to avoid cache misses, but this would often require knowledge of the user (traits template parameter maybe).

For a very good implementation of memory pools, look at: Boost.Pools, that site also have some good info about memory pools, especially simple seggregated storage. And it contains lots of keywords you could use for some Google searches.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this