Click here to Skip to main content
11,926,982 members (51,563 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked

A sample of memory pool

, 26 Mar 2007
Rate this:
Please Sign up or sign in to vote.
Memory Pool


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


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


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


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


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


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


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


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


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


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

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


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



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


About the Author

Bony Chen
Web Developer
China China
Bony Chen, a senior software engineer, specializes in C++, COM, C#,, 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

You may also be interested in...

Comments and Discussions

GeneralRe: IMHO Pin
lyle leng24-Sep-08 0:09
memberlyle leng24-Sep-08 0:09 
GeneralA trip to OOP land is needed Pin
norm .net26-Mar-07 5:59
membernorm .net26-Mar-07 5:59 
Would be better to encapsulate Allocate inside the Contructor of BufferPool and a Destuctor to call Release?
A trypical pattern would be...

BufferPool bfA(1024,20); // This automatically calls Allocate and throws an exception if failed.

// Do something with bfA

// Add a destructor to BufferPool which automatically call Release()

catch (BufferPoolException bpe)
// Show message

.net is a box of never ending treasures, every day I get find another gem.

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 26 Mar 2007
Article Copyright 2007 by Bony Chen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid