Re: Memory leaks in STD::QUEUE
Hari Shankar wrote:
Hello
I am using std::queue for storing structures of size 15k. I have 2 threads
to write and read to/from the queue.
When i profile it using Compuware DevPartner, i found out that POP is not
really removing the allocated memory.
No, but it does make it available to the next PUSH.
Because of that my application is
crashing as the memory overflow occurs.
Can anyone hint me what's wrong
The memory requirements of the deque (which is used internally by
default in std::queue) will be proportional to the maximum size it
reaches - nodes in the deque do not get released but rather recycled
(this is in addition to any memory caching done by the memory allocator
you are using).
So, as long as the maximum size the deque reaches is manageable, you
shouldn't have a problem.
As mentioned by SvenC, the problem might be due to not synchronising
access to the queue.
Finally, if you want to release all the cached queue nodes, you need to
use a derived class of std::queue, and add a member something like this:
void releaseExcessMemory()
{
typedef std::queue<T>::container_type cont_t;
//c is a protected member of queue, and is the underlying container
//shrink to fit swap idiom:
cont_t(c.begin(), c.end(), c.get_allocator()).swap(c);
}
Tom