473,508 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Suspected Memory Leak in Multithread queue implmenetation

Hi Guys,

I've done a mulithread queue implementation on stl<queue>, my
developement environment is on VC6 as well as FC3. Let's talks about
the win32 side.

The suspected memory leak is find after I've run through my unit test
cases.

Test Case:
1. start 2 threads, thread A and thread B.
2. thread A enqueue 10000 dummy objects, meanwhile thread B dequeue
10000 dummy objects (delete object at the same time), these operations
are called a "cycle".
3. there is a while loop to loop the cycle, in between each cycle,
there is a 5 seconds sleep time.
4. run this for 10 minutes
5. stop insert any dummy objects

I've run this test case for 15 mins, and monitor its private_bytes,
working_set and virtual_bytes with the windows performance monitor.

My concern after running this test case is that,

1. virtual_bytes increase double its size frequently (seems to me it is
due to the queue memory allocation), and the virtual_bytes never drops
into its original position (I mean at the beginning when the program is
started)
2. both private_bytes and working_set are increasing as well, just the
fact that, duing the 5 seconds wait time, (step 3), these values drop
to nearly zeros, but after the 5 seconds wait time, these values
increase rapidly back to a state where it was dropped. and, of course
keep increasing.
3. during the (step 5), I can tell virtual_bytes is at its max, and
private_bytes and working_set are set at a values (1/3 below the max
point) and won't be able to drop any more.

My questions are:

1. shall windows OS handle the virtual_bytes, private_bytes and
working_bytes dellocation?
2. or is it because there is error on my implementation of multithread
queue?

Thank you very much. I am really appreciate yours help,

Best wishes,

Kit

--------------- My queue --------------------------

#ifndef __QUEUELOCK_HEADER__
#define __QUEUELOCK_HEADER__

#ifdef WIN32
# define _WINSOCKAPI_
# include <Windows.h>
#else
extern "C" {
# include <pthread.h>
};
#endif

#include <queue>
#include <iostream>

#define EXIT_IF(EXP) if (EXP) { cout << "Failed qlock: " #EXP "\n";
exit(1); }

#define QUEUE_MAX_SIZE 30000
using namespace std;

/* Queue with a lock, a template extending queue template
*/
template <typename _Tp, typename _Sequence = deque<_Tp
class qlock : public queue<_Tp,_Sequence{
typedef typename _Sequence::value_type value_type;
typedef typename _Sequence::reference reference;
typedef typename _Sequence::const_reference const_reference;
CRITICAL_SECTION CriticalSection;// Lock for modification
HANDLE hSynEvent; // Empty queue
condition
public:
qlock() {
// Initialize the critical section one time only.
InitializeCriticalSection(&CriticalSection);
hSynEvent = CreateSemaphore(NULL, 0, QUEUE_MAX_SIZE,
NULL);
if (hSynEvent == NULL)
{
printf("CreateEvent failed: %d\n",
GetLastError());
exit(1);
}
};
~qlock() {
// Release resources used by the critical section
object.
DeleteCriticalSection(&CriticalSection);
EXIT_IF(CloseHandle(hSynEvent));
};
void enqueue(const value_type& __x) {
EnterCriticalSection(&CriticalSection); // Lock the
queue
if (this->size() QUEUE_MAX_SIZE)
{
while(this->size() 1) // Clear the queue
{
if(!this->empty()) {
value_type r;
r = this->front();
this->pop();
if(r != NULL) delete r;
}
}
}
this->push(__x);
// Release Semaphore
ReleaseSemaphore(hSynEvent, 1, NULL);
LeaveCriticalSection(&CriticalSection); // Unlock
the queue
};
//reference
value_type dequeue(int i=0,int j=0) { // Blocking-call
bool empty = true;
value_type r = {0};
EnterCriticalSection(&CriticalSection);
empty = this->empty();
if(!empty) {
r = this->front();
this->pop();
}
LeaveCriticalSection(&CriticalSection);
while (empty) {
// Wait until something is written into the
queue
if(WaitForSingleObject(hSynEvent, INFINITE) ==
WAIT_FAILED)
{
printf("WaitForSingleObject failed
(%d)\n", GetLastError());
exit(1);
};
EnterCriticalSection(&CriticalSection);
empty = this->empty();
if(!empty) {
r = this->front();
this->pop();
}
LeaveCriticalSection(&CriticalSection);
};
return r;
}
const_reference dequeue() const { // Blocking-call
bool empty = true;
const_reference r;
EnterCriticalSection(&CriticalSection);
empty = this->empty();
if(!empty) {
r = this->front();
this->pop();
}
LeaveCriticalSection(&CriticalSection);

while (empty) {
// Wait until something is written into the
queue
if(WaitForSingleObject(hSynEvent, INFINITE) ==
WAIT_FAILED)
{
printf("WaitForSingleObject failed
(%d)\n", GetLastError());
LOG(LV_EMERG,"Wait for Single Queue
Object failed.",1);
exit(1);
};
EnterCriticalSection(&CriticalSection);
empty = this->empty();
if(!empty) {
r = this->front();
this->pop();
}
LeaveCriticalSection(&CriticalSection);
};
return r;
}
int qSize() {
int size = 0;
EnterCriticalSection(&CriticalSection);
size = this->size();
LeaveCriticalSection(&CriticalSection);
return size;
}
};

Jan 15 '07 #1
0 1119

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1122
by: Al Franz | last post by:
I believe there is a memory leak in cPickle. I am using python2.2. I have a parallel code which uses array() and indices() from Numeric to massage data buffers before being sent and received by...
3
4653
by: Jeremy Lemaire | last post by:
Hello, I am working on cross platform code that is displaying a huge memory leak when compiled on 11.00 HPUX using the aCC -AA flag. It is not leaking on NT, LINUX, Solaris, or HPUX without the...
4
3515
by: Jeronimo Bertran | last post by:
I have a multithreaded application that manipulates a Queue: protected Queue outputQueue; when I add elements to the queue I use a critical section private void Add(OutputRecord record) {...
17
4770
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
25
2346
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
23
4509
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
7
1714
by: Roman Petrichev | last post by:
Hi folks. I've just faced with very nasty memory consumption problem. I have a multythreaded app with 150 threads which use the only and the same function - through urllib2 it just gets the web...
2
5280
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find...
3
2552
by: Godzilla | last post by:
Hello, I have a program that create and pop an object off a queue, but it is experiencing some memory leakage. I have been unable to detect where the memory leakage occur. The strange thing is...
2
4085
by: manojmohadikar2008 | last post by:
Hi All, We are observing a serious issue with the memory usage of Queue and its very critical issue which needs to be fixed. We have an application which runs two threads i.e. a Producer and a...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.