Preventing Memory Fragmentation in C++

Started by
7 comments, last by Eric_B 19 years, 2 months ago
Any tips, 3rd party libs, or other options? Thanks
Advertisement
Which are good libraries for memory pool management and garbage compacting? Give me both free and commerical.

BC++B ships with memory pool management and garbage compacting classes?

I heard VC++2005 will come with memory management classes like GC? Is that true?
if you use C++ with the managed extensions, then yes you'll be able to use GC'd memory.

As for your other question, Boost comes with a pool memory allocator
I've just been playing around with the Boost pool library mentioned above, and it works great!
thank you both :D
You may consider looking into templated free lists. I first read about them in one of the Game Programming Gem books. They're light weight, uber fast, and simple as pie.

They work by creating a contigious block of resources. It then uses a stack to manage the wouldbe allocs and deallocs of memory. They're only applicable if you have many of the same type of objects though. Below is the TFreeList I wrote use in all my code.

You'd create a list of ints as so-

<code>
TFreeList<int> list(100); // Create 100 ints

int* lpInt = list.newInstance(); // Get an instance
// Use it in here to our hearts content
list.freeInstance(lpInt); // Free it when we're done
</code>


<code>

template <class Class_Type>
class TFreeList
{
private:
Class_Type* lpObj;
Class_Type** lpObjTable;
int iTop;

public:
int iNumObjects;

TFreeList(int iNumObjects);
~TFreeList();

Class_Type* newInstance();
void freeInstance(Class_Type* lpObj);
};

template <class Class_Type>
TFreeList<Class_Type>::TFreeList(int iNumObjects)
{
this->iNumObjects = iNumObjects;

lpObj = new Class_Type[iNumObjects];
lpObjTable = new Class_Type*[iNumObjects];
for (int cnt=0;cnt<iNumObjects;cnt++)
lpObjTable[cnt] = &lpObj[cnt];

iTop = iNumObjects;
}

template <class Class_Type>
TFreeList<Class_Type>::~TFreeList()
{
delete[] lpObj;
delete[] lpObjTable;
lpObj = NULL;
lpObjTable = NULL;
}

template <class Class_Type>
Class_Type* TFreeList<Class_Type>::newInstance()
{
if (--iTop < 0)
{
iTop = 0;
return NULL;
}

return lpObjTable[iTop];
}

template <class Class_Type>
void TFreeList<Class_Type>::freeInstance(Class_Type* lpObj)
{
lpObjTable[iTop++] = lpObj;
}

</code>
One more thing, this will work with std::string that comes with VC++?
I dont think it will directly, std::string is just a typedef of std::basic_string iirc, so you'll have to make a new typedef with the same traits as std::string but change the memory allocator to a pool allocator (assuming that can be done, I would assume so but i've never really looked into it)
"They're only applicable if you have many of the same type of objects though. Below is the TFreeList I wrote use in all my code."
Thanks for the code, and my only trouble is list of std::string for now. The program simply keeps a list of memos on the server, but the length of the list is unpredictable... it can grow or shrink very quickly... and I don't want to restart the server everyday.

This topic is closed to new replies.

Advertisement