Click here to Skip to main content
12,290,502 members (30,123 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked

Memory Leak Caused by Fragmentation

, 23 Feb 2006
Rate this:
Please Sign up or sign in to vote.
This article shows the effect of Memory Fragmentation and provides a generic object pool class.


Some days ago, I finished fixing a bug called "memory leak" that I think I'll never forget during my career.

My program was about a real-time monitor that read data from a server and viewed graphs. My program met the "memory leak" problem when data throughput was too heavy. I remember, every 100 ms the program had to read about 2 Kb data. And then, after only a day, my program ran out of virtual memory although the private bytes still looked pretty good on the Performance tool.

I tried to detect memory leak by using a lot of tools, from free of charge tools to trade tools as I was used to do before, but none of the tools could find out any memory leak.



Now, let's consider the problem:

//The following was a global buffer
CPtrArray g_oBuffer;

//Any time new data arrived, I allocated
//a new memory and added it to the buffer
MyData* oData = new MyData();

//When old data wasn't needed anymore, I removed it
MyData* oData = (MyData*)g_oBuffer.GetAt(0);
delete oData;

Everything looked fine and that was the reason why my program's private bytes looked stable. But unfortunately, after only a day, I met a memory exception and saw the virtual bytes reach the 2 GB limitation.

The Performance monitor looked like below:

Sample Image


Instead of allocating memory one by one, I decided to pre-allocate a large amount of memory into an object pool and use it when necessary. Then, my code became like this:

//Any time new data arrived, I got a pre-allocated memory
//from the object pool and added it to the buffer

//MyData* oData = new MyData();
MyData* oData = (MyData*)MyObjectPool::GetNew();

//When old data wasn't needed anymore, I removed it
//and returned the memory to the object pool
MyData* oData = g_oBuffer.RemoveAt(0);
//MyData* oData = g_oBuffer.RemoveAt(0);

After applying this solution, I was impressed with the result as below:

Sample Image

Using the code

In order to provide a generic object pool, I re-implemented the object pool as a template class. With this article, I'd just like to show a real experience on Memory Fragmentation and a solution which has been applied successfully. Besides, I only did a few tests on my code. Therefore, it may still have some bugs remaining. If anyone could find out a bug, please send a feedback to me. I will really appreciate it.

To use this generic object pool, all you have to do is:

#define StrPool CObjectPoolImpl<CString>

//Create a new pool
StrPool MyPool(10); //Size in Mega byte
//Create a new CString object
CString* pStr = MyPool.getNewObj();
//Delete CString object when it's not used any more
//Empty the pool


From the bottom of my heart, I would like to say "thanks" to the author Danny Kalev who has written this article. This one was very short but cool enough to describe what a memory fragmentation is.


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

Program Manager Harvey Nash
Vietnam Vietnam
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: Clean up the original code and no leak Pin
blackdat783-Aug-05 16:43
memberblackdat783-Aug-05 16:43 
GeneralInteresting article Pin
Anonymous2-Aug-05 7:44
sussAnonymous2-Aug-05 7:44 
GeneralRe: Interesting article Pin
blackdat782-Aug-05 16:25
memberblackdat782-Aug-05 16:25 
GeneralRe: Interesting article Pin
melwyn3-Aug-05 0:20
membermelwyn3-Aug-05 0:20 
GeneralThx, Very Useful Pin
Hing30-Jul-05 21:25
memberHing30-Jul-05 21:25 
GeneralConsider using LFH instead. Pin
yafan29-Jul-05 3:33
memberyafan29-Jul-05 3:33 
GeneralRe: Consider using LFH instead. Pin
Blake Miller29-Jul-05 4:24
memberBlake Miller29-Jul-05 4:24 
GeneralRe: Consider using LFH instead. Pin
Anonymous3-Aug-05 1:50
sussAnonymous3-Aug-05 1:50 
GeneralRe: Consider using LFH instead. Pin
erushton15-Aug-05 3:27
membererushton15-Aug-05 3:27 
QuestionDoes this actually work for CStrings? Pin
Luuk Weltevreden28-Jul-05 22:09
memberLuuk Weltevreden28-Jul-05 22:09 
AnswerRe: Does this actually work for CStrings? Pin
slim29-Jul-05 1:26
memberslim29-Jul-05 1:26 
AnswerRe: Does this actually work for CStrings? Pin
armentage29-Jul-05 8:17
memberarmentage29-Jul-05 8:17 
AnswerRe: Does this actually work for CStrings? Pin
blackdat7831-Jul-05 19:58
memberblackdat7831-Jul-05 19:58 
GeneralVery interesting Pin
Stlan28-Jul-05 21:07
memberStlan28-Jul-05 21:07 
GeneralRe: Very interesting Pin
gnk31-Jul-05 14:01
membergnk31-Jul-05 14:01 
GeneralRe: Very interesting Pin
John M. Drescher25-Oct-05 7:03
memberJohn M. Drescher25-Oct-05 7:03 

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
Web02 | 2.8.160518.1 | Last Updated 23 Feb 2006
Article Copyright 2005 by blackdat78
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid