Click here to Skip to main content
15,881,173 members
Articles / Mobile Apps
Article

VMemPool - Virtual Memory Pool Management class

Rate me:
Please Sign up or sign in to vote.
4.80/5 (18 votes)
8 Aug 2002CPOL2 min read 153.6K   3.4K   70   34
If you deal with same size objects in server coding, VMemPool shows a good way for beginners.

Image 1

Introduction

As you know, new/delete operations take a lot of CPU time. If you work with servers, CPU time is important. If additional memory is added to the server, then the servers' available memory size will grow in a linear fashion. However CPU's don't behave the same (dual CPU's doesn't necessarily mean twice the speed of a one CPU situation.)
So common server code has it's own efficient memory management system. VMemPool is the one of them for me.

About The Implementation

CVMemPool is generic (template) class since I assumed the client would want a similar usage as new/delete. So, using CVMemPool, you can code like it's a general pointer.

CObj* p = new CObj;

p->do();

delete p;

CVMemPool has it's own 'allocation table' implemented using a circular queue, so you can check if a pointer in a pool is valid using vmIsBadPtr. You can also check how many objects are allocated in the pool using vmGetPoolInfo.

CVMemPool has two template variable, class objT and DWORD _dwPoolSizeT = 1000. _dwPoolSizeT is the size of the pool. You can reconfigure the pool size with this variable. objT should not be important to you. If objT is absent and you have a different class make the object as below.

// suppose CVMemPool is like below. it's not real code.
template <DWORD _dwPoolSizeT = 1000>
class CVMemPool 
{ 
  ... 
};

class CObj1 : public CVMemPool<>
{
 ...
};

class CObj2 : public CVMemPool<>
{
 ...
};

CObj1 c1;
CObj2 c2; // it will share pool with c1 , it is not good. cos i need objT.

As you know, when a compiler sees the last instancing code, the compiler will think c1 and c2 are the same template class layout and so it make only one virtual pool (because CVMemPool<T,F>::ms_pMemPool is static.)

Usage

//make class in pool.
class CObj : public CVMemPool<CObj>
{
 ...
};

// and you can use it same like general new/delete code.
CObj* p = new CObj; // Pool is created, and allocation in first pool block.
CObj* p2 = new CObj; // second pool block will be used.

delete p; // first block will be freed.
delete p2; // second ,too.

Performance

Test environment

P4 1.6GHz, 256MB ram, Windows 2000 Professional, release executable testing.
Two situations tested:
first, CObj is 1,000 bytes size and loop new and delete 10,000 , 20.000 ....
first, CObj is 10,000 bytes size and loop new and delete 10,000 , 20.000 ....
( n * 1,000 is wrong, n* 10,000 is right, sorry ) The Results are below. Image 2 Image 3

I can't say that these results are exactly right, but I think CVMemPool will be better than the default heap operation (new/delete) on the server side, or on the client side for some CPU's.
In the second situation, I tested 6,000 or over, but I couldn't see the result on the 'Heap' because the program gave a fatal error - insufficient memory - surely, CVMemPool works well and fast. :)

I hope it help you. Thanks a lot!

Revision History

14 Aug 2002 - Initial revision

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Korea (Republic of) Korea (Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to alloc object array? Pin
apollo.hao10-Nov-11 21:37
apollo.hao10-Nov-11 21:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.