Click here to Skip to main content
Licence CPOL
First Posted 8 Aug 2002
Views 105,113
Downloads 2,136
Bookmarked 62 times

VMemPool - Virtual Memory Pool Management class

By Cho, Kyung-min | 8 Aug 2002
If you deal with same size objects in server coding, VMemPool shows a good way for beginners.

1

2

3
4 votes, 25.0%
4
12 votes, 75.0%
5
4.76/5 - 29 votes
μ 4.37, σa 0.78 [?]

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

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)

About the Author

Cho, Kyung-min

Web Developer

Korea (Republic Of) Korea (Republic Of)

Member
I born in 1978 and live in korea.
I have to study hard to be better programmer. if my shy code help someone, then i hope share it with pleasure. My nick is 'bro' ,(that is) short for 'brother'. i like it cuz feel friendly. Thanks for reading.

I'm working for 5yrs in company. now in samsung electronics.

my first c 'hello world' is in 1995. and now my interesting is about serverside stuff( iocp, 3tier, etc.) and rtos.


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhow to alloc object array? Pinmemberapollo.hao22:37 10 Nov '11  
Generalhere is a serious semantic problem about multilayer inheritor [modified] PinmemberHarly20:05 8 Nov '10  
General안녕하세요 이곳에서 같은 나라 분이 짠 코드를 보게되니 재미있네요. PinmemberMatrix is everywhere17:15 24 Aug '10  
GeneralNext small bug found... [modified] Pinmemberlefis10:53 27 Sep '08  
Questionmemory problem PinmemberKumara Swamy2:27 12 May '07  
Generalproblem Pinmembercaishaikedou18:35 27 Apr '06  
GeneralAlign with 4 bytes PinmemberAloneInTheCP22:13 10 Jan '06  
AnswerRe: Align with 4 bytes Pinmemberrinzai9:34 5 Mar '07  
GeneralVS.NET 2003 Pinmemberprofix8987:17 7 Feb '05  
GeneralRe: VS.NET 2003 Pinmemberrabidusprocella0:33 11 May '05  
QuestiontimeGetTime? Pinmembermadkoala20:31 12 Jan '05  
AnswerRe: timeGetTime? PinsussAnonymous19:47 14 Jan '05  
GeneralBitset PinmemberRonma0:04 28 Dec '04  
GeneralRe: Bitset PinmemberCho, Kyung-min10:09 1 Jan '05  
GeneralGreat code. Small bug found, and enhancement suggestion. Pinmemberprcarp6:48 14 Jan '04  
GeneralRe: Great code. Small bug found, and enhancement suggestion. PinmemberCho, Kyung-min15:27 14 Jan '04  
GeneralRe: Great code. Small bug found, and enhancement suggestion. Pinmemberrobosport17:52 19 Jul '04  
GeneralRe: Great code. Small bug found, and enhancement suggestion. Pinmemberprcarp10:05 20 Jul '04  
GeneralRe: Great code. Small bug found, and enhancement suggestion. PinmemberHarly15:34 8 Nov '10  
GeneralIssues with the design PinmemberVictor Boctor19:02 19 Nov '02  
I like the idea of the class, specially the transparency to the user of the CObj class. However, I have some issues with the design of this class (or the way the article explains how to use the class):
 
1. The is-a relation for CObj and VMemPool fails, i.e. CObj is not a memory pool. For example, CEmployee is not a VMemPool.
 
2. The need for inheritance will most probably cause Multiple Inheritance issues, since the CEmployee will have to inherit from CPerson and VMemPool, where CPerson might also have inherited from VMemPool. Assuming that CPerson is not an abstract class.
 
3. To make use of this memory pool, you will need to change the code of the class being allocated. In some cases this is not desirable. For example, if your class is a container which is allocating 100000 instances of a class that is implemented in a 3rd party library. The design should be flexible enough to allow using this techique for the class under development or for a class that is used by the class under development.
 
My suggestion is to be able to use the class in this way:
 

// A sample where the memory pool is implemented as part of the class.
class CEmployee
{
// define new / delete to map to m_MemPool new / delete
// a macro can be defined to automatically do that
// #define USE_MEM_POOL(class,mempool)
// in this case USE_MEM_POOL(CEmployee, m_MemPool);
 
private:
static VMemPool m_MemPool;
}

 

// A sample where the memory pool is implemented as part of a collection
// class.
class CMyList
{
// in this case the list should use m_3rdPartyMemPoolA to new or
// delete instances of C3rdPartClassA. In this case it is not
// as transparent, but it is possible.
 
private:
VMemPool m_3rdPartyMemPoolA;
VMemPool m_3rdPartyMemPoolB;
}

 
These are just some thoughts that I wanted to share after reading the article and having a quick look at the code. I have to admit that I didn't get a chance yet to actually use the code. Unsure | :~
 
http://webnotes.sf.net[^]
GeneralRe: Issues with the design PinmemberTim Smith11:15 22 Nov '02  
GeneralRe: Issues with the design PinmemberCho, Kyung-min21:23 16 Dec '02  
GeneralNo memory is actually allocated PinmemberTim Smith10:24 5 Nov '02  
GeneralLea Allocator PinmemberTim Smith10:38 5 Nov '02  
GeneralRe: No memory is actually allocated PinmemberCho, Kyung-min21:15 16 Dec '02  

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.

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