Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Does STL Queue release memory?

1,319 views
Skip to first unread message

Bilal Anjum

unread,
Nov 28, 2002, 11:10:21 AM11/28/02
to
Hi All!
I am using STL Queue of data type of my own defined struct(struct
consists of STL string data type members). when i push elemnets in the
queue, i have seen the increase in memory of the program continously.
Although i am coninously monitoring the queue size and most of the time its
zero coz i m poping the pushed elements from the queue. but the memory size
never seems to drop on Win2k task manager. I have tested this scenario with
just using push and pop functions but still the same so there are no more
apparent memory leaks.
So does the STL queue releases the momory when i use POP command or i
have to do this myself?
Please reply ASAP :)
Regards
Bilal Anjum


Carl Daniel [MVP]

unread,
Nov 28, 2002, 11:33:01 AM11/28/02
to

"Bilal Anjum" <ulove...@hotmail.com> wrote in message
news:uiacNivlCHA.2456@tkmsftngp04...

Yes, queue takes care of itself. By default, std::queue uses std::deque
internally. std::deque uses a linked list of buffers to store it's
contents. Over the course of a program's life, it shouldn't be surprising
to see the memory consumed by a queue rise a little bit, but if you're
seeing continual growth, there's something else wrong in your program.

If you can post a complete program that demonstrates your problem, someone
can probably tell you what's wrong.

-cd


Bilal Anjum

unread,
Nov 28, 2002, 1:47:41 PM11/28/02
to
Hi!
Thnx for the reply. Here is the code:
This is my Queue Definition
typedef queue<string> PacketQueue;

I use this command to push the data in a queue.
EnterCriticalSection(&css);
SrvPool.push(strPacket);
LeaveCriticalSection(&css);

I use this command to pop the data from queue in a seperate worker thread.
EnterCriticalSection(&css);
SrvPool.push(strPacket);
LeaveCriticalSection(&css);

Everything else is related to processing of the packet. and i have once
removed all the code and just used the above mentioned code but still memory
is going up.
You can judge this as in one thread i am pushing the packets in a queue at
the rate 300 packets per second and in an other thread i used to pop the get
the packet from queue using this code
EnterCriticalSection(&css);
strPacket = SrvPool.front();
LeaveCriticalSection(&css);

and then i use the above mentioned code to pop the packet from the queue.
And after some time (tested many times) i process over 100,000 packets using
the above code. each time using the above mentioned cycle and all i get is
109,000 kb of memory in Win2k Task manager against my process. Later it was
just 26,000 k for approx 250,000 packets and crashed :( (i have changed a
little bit of logic). Does this memory get free automatically when my
program terminates after some time or i have to do this myself? Also my CPU
usage goes to 100% for 5 to 6 minutes until it gets back to normal. So any
suggetions, how to improve my program and avoid the crash situation. I have
approx 3 worker threads monitoring there own STL Queue and processing it?
Plz tell me how can i improve my program? This is Client server
architecture, and i am using Intel P-3 450 Mhz with 512 MB RAM. (Some of
thses are later findings while i was writing this answer and analyzing the
application).

Waiting for ur Reply
Regards
Bilal Anjum


Markus Mauhart

unread,
Nov 29, 2002, 8:45:11 AM11/29/02
to
IMHO the symptom of physical/virtual memory not beeing
released as soon as one would like is probably not
related with queue or string or new, but with new's
backend. Normally this is a win32 heap, CreateHeap'ed
at startup by the CRTL and closed on exit.
Any code in a process can create and use additional
win32 heaps. Each such heap on demand reserves big
chunks of VM and then on demand commits pages.

My experience is that this heap does not automatically
release reserved virtual memory that is no more necessary,
but it decommits unused pages of its reserved VM range
more or less automatically.

IMHO it's not unlikely that win32 automatically shrinks
some heaps VM when a process runs out of VM.

Exact behaviour might depend on other heap options
or usage pattern, for the options check the doku for
HeapXXX functions; different CRTL options might
choose different Heap options.

There is also HeapCompact() or a similar function,
this might release unused VM. The CRTL has a variable
holding the heap HANDLE, + functions for getting it.
Declare this things in your source and then you can
access/compact the heap directly.

Alternatively you can replace ::operator new()/delete()
through functions accessing your own global win32-heap
variable/handle. std::allocator then uses your
functions, therefore string and queue too.

Alternatively you can make strings and queues use
your own win32 heap: make a copy of std::allocator<>,
rename to my_allocator, replace its calls to
::operator new()/delete() functions through your
functions accessing your win32 heap HANDLE,
then use it through ...

typedef basic_string <char ,my_alloc<char> > my_string;
typedef queue <my_string ,my_alloc<my_string> > my_queue;


Regards,
Markus.

Markus Mauhart

unread,
Nov 30, 2002, 12:33:58 AM11/30/02
to
another psbl solution ...

#include <malloc.h>
int _heapmin();

The _heapmin function minimizes the heap by releasing
unused heap memory to the operating system.


Bilal Anjum

unread,
Dec 1, 2002, 10:45:01 PM12/1/02
to
Hi!
Thnx Markus for ur reply. It has really helped us in solving our problem
:)
Regards
Bilal Anjum


0 new messages