Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / C++
Article

A sample of memory pool

Rate me:
Please Sign up or sign in to vote.
1.48/5 (7 votes)
26 Mar 20071 min read 28.7K   972   20   2
Memory Pool

Introduction

Memory allocation in C or C++ language can take lots of time, especially in some efficient software product. If a application always allocate memory, its performance decreases greatly when it is running over a long time. The heap becomes fragments.

the solution to improve the efficiency of memory allocation is using memory pool. It will allocate a big amount of memory on application startup and it will be separate into smaller memory buffer uints. Every time you request memory from the pool, it is taken from previously allocated memory buffer uints, and not from OS. The biggest advantages are

1. Faster than normal memory allocation like malloc/new operator in C,C++ language.

2. very litter heap-fragmention

How does it work?

On application startup, you will allocate a big amount of memory. The big memory block is made up of many buffer units. If the size of buffer that you allocate is smaller than the buffer unit size, the buffer pool will return you the free buffer unit point. One buffer unit is made up of the size of the buffer unit and the buffer memory that you can get.

Using the code

C++
//
// 

<p style="TEXT-ALIGN: left" align="left">    size_t initSize = 1024;

</p>

<p style="TEXT-ALIGN: left" align="left">    //Initialize the memory buffer

</p>

<p style="TEXT-ALIGN: left" align="left">    BufferPool bfA(initSize,20);

</p>

<p style="TEXT-ALIGN: left" align="left">  //allocate memory from the memory pool

</p>

<p style="TEXT-ALIGN: left" align="left">    char *p = bfA.Allocate(100);

</p>

<p style="TEXT-ALIGN: left" align="left">    if(p)

</p>

<p style="TEXT-ALIGN: left" align="left">    {

</p>

<p style="TEXT-ALIGN: left" align="left">  //use the memory buffer

</p>

<p style="TEXT-ALIGN: left" align="left"> 
</p>

<p style="TEXT-ALIGN: left" align="left">  //release memory to the memory pool

</p>

<p style="TEXT-ALIGN: left" align="left">       bfA.Release(p);
    }



</p>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
Bony Chen, a senior software engineer, specializes in C++, COM, C#, ASP.net, ISAPI. He has nearly seven years of software development experience, and has successfully developed many influential software products, such as SPES, CSO, QQ (a famous IM tool in China).
And now he is focusing on P2P technology. He aims at being an expert in software development, software consulting, software components and computer science.
If you have any opinions or questions in software area, please contact bonyren@163.com.

Comments and Discussions

 
GeneralRe: IMHO Pin
lyle leng23-Sep-08 23:09
lyle leng23-Sep-08 23:09 
GeneralA trip to OOP land is needed Pin
NormDroid26-Mar-07 4:59
professionalNormDroid26-Mar-07 4:59 

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.