Click here to Skip to main content
11,721,053 members (76,670 online)
Click here to Skip to main content

VMemPool - Virtual Memory Pool Management class

, 8 Aug 2002 CPOL 125.4K 3K 69
Rate this:
Please Sign up or sign in to vote.
If you deal with same size objects in server coding, VMemPool shows a good way for beginners.
<!-- Download Links --> <!-- Article image -->

<!-- Add the rest of your HTML here -->

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.

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. Smile | :)

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)

Share

About the Author

Cho, Kyung-min
Software Developer
Korea (Republic Of) Korea (Republic Of)
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionhow to alloc object array? Pin
apollo.hao10-Nov-11 21:37
memberapollo.hao10-Nov-11 21:37 
Generalhere is a serious semantic problem about multilayer inheritor [modified] Pin
Harly8-Nov-10 19:05
memberHarly8-Nov-10 19:05 
General안녕하세요 이곳에서 같은 나라 분이 짠 코드를 보게되니 재미있네요. Pin
Matrix is everywhere24-Aug-10 16:15
memberMatrix is everywhere24-Aug-10 16:15 
GeneralNext small bug found... [modified] Pin
lefis27-Sep-08 9:53
memberlefis27-Sep-08 9:53 
Questionmemory problem Pin
Kumara Swamy12-May-07 1:27
memberKumara Swamy12-May-07 1:27 
Generalproblem Pin
caishaikedou27-Apr-06 17:35
membercaishaikedou27-Apr-06 17:35 
GeneralAlign with 4 bytes Pin
AloneInTheCP10-Jan-06 21:13
memberAloneInTheCP10-Jan-06 21:13 
AnswerRe: Align with 4 bytes Pin
rinzai5-Mar-07 8:34
memberrinzai5-Mar-07 8:34 
GeneralVS.NET 2003 Pin
profix8987-Feb-05 6:17
memberprofix8987-Feb-05 6:17 
GeneralRe: VS.NET 2003 Pin
rabidusprocella10-May-05 23:33
memberrabidusprocella10-May-05 23:33 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/vcerrCompilerWarningLevel1C4346.asp[^]

it's standard
QuestiontimeGetTime? Pin
madkoala12-Jan-05 19:31
membermadkoala12-Jan-05 19:31 
AnswerRe: timeGetTime? Pin
Anonymous14-Jan-05 18:47
sussAnonymous14-Jan-05 18:47 
GeneralBitset Pin
Ronma27-Dec-04 23:04
memberRonma27-Dec-04 23:04 
GeneralRe: Bitset Pin
Cho, Kyung-min1-Jan-05 9:09
memberCho, Kyung-min1-Jan-05 9:09 
GeneralGreat code. Small bug found, and enhancement suggestion. Pin
prcarp14-Jan-04 5:48
memberprcarp14-Jan-04 5:48 
GeneralRe: Great code. Small bug found, and enhancement suggestion. Pin
Cho, Kyung-min14-Jan-04 14:27
memberCho, Kyung-min14-Jan-04 14:27 
GeneralRe: Great code. Small bug found, and enhancement suggestion. Pin
robosport19-Jul-04 16:52
memberrobosport19-Jul-04 16:52 
GeneralRe: Great code. Small bug found, and enhancement suggestion. Pin
prcarp20-Jul-04 9:05
memberprcarp20-Jul-04 9:05 
GeneralRe: Great code. Small bug found, and enhancement suggestion. Pin
Harly8-Nov-10 14:34
memberHarly8-Nov-10 14:34 
GeneralIssues with the design Pin
Victor Boctor19-Nov-02 18:02
memberVictor Boctor19-Nov-02 18:02 
GeneralRe: Issues with the design Pin
Tim Smith22-Nov-02 10:15
memberTim Smith22-Nov-02 10:15 
GeneralRe: Issues with the design Pin
Cho, Kyung-min16-Dec-02 20:23
memberCho, Kyung-min16-Dec-02 20:23 
GeneralNo memory is actually allocated Pin
Tim Smith5-Nov-02 9:24
memberTim Smith5-Nov-02 9:24 
GeneralLea Allocator Pin
Tim Smith5-Nov-02 9:38
memberTim Smith5-Nov-02 9:38 
GeneralRe: No memory is actually allocated Pin
Cho, Kyung-min16-Dec-02 20:15
memberCho, Kyung-min16-Dec-02 20:15 
GeneralMem allocation Pin
bk27-Aug-02 18:26
memberbk27-Aug-02 18:26 
GeneralRe: Mem allocation Pin
Cho, Kyung-min29-Aug-02 14:35
memberCho, Kyung-min29-Aug-02 14:35 
GeneralSmartheap Pin
Simon Capewell21-Aug-02 2:53
memberSimon Capewell21-Aug-02 2:53 
GeneralRe: Smartheap Pin
drunin5-May-04 6:50
memberdrunin5-May-04 6:50 
GeneralRe: Smartheap Pin
Simon Capewell5-May-04 8:03
memberSimon Capewell5-May-04 8:03 
GeneralGreat performance! Pin
Albert van Peppen14-Aug-02 5:57
memberAlbert van Peppen14-Aug-02 5:57 
GeneralRe: Great performance! Pin
Cho, Kyung-min15-Aug-02 15:24
memberCho, Kyung-min15-Aug-02 15:24 
GeneralImprovment to BitSet.{cpp|h} Pin
Matthias Mann9-Aug-02 7:45
memberMatthias Mann9-Aug-02 7:45 
GeneralRe: Improvment to BitSet.{cpp|h} Pin
Cho, Kyung-min11-Aug-02 15:45
memberCho, Kyung-min11-Aug-02 15:45 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150901.1 | Last Updated 9 Aug 2002
Article Copyright 2002 by Cho, Kyung-min
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid