help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory Leak??


From: John V. Shahid
Subject: Re: Memory Leak??
Date: Tue, 28 Aug 2007 21:46:02 -0400

On Mon, 2007-08-27 at 09:25 -0700, nandakiran@gmail.com wrote:
> i tried valgrind in combination with the GLIBCXX_FORCE_NEW option.
> valgrind
> says i have freed all the the heap blocks, but still the memory
> occupied by the program
> doesn't reduce after doing the free.
> (i have increased the loop count from 10,000 to 100,000)
> here is the output:

Then you don't have any memory leaks. I'll assume that you are looking
at 'sysguard' output, or anything similar which displays the size of the
heap of a certain process. To understand why this is happening, you have
to first understand how malloc/free works. When you call malloc to
allocate a block of memory, malloc looks for a free block (i.e. a block
that was allocated and freed before), if it didn't find one then it will
increase the heap using the 'brk' system call and returns a pointer to
the new block. Later when you free a block of memory, free will save the
address and size of that block to be used in further allocations. The
trick here is whether 'free' will return the memory to the operating
system or not. As far as I know GNU systems don't do this, and even if a
system do that, it's not always possible due to memory fragmentation.
The result is that the heap will increase and never decrease after you
allocate a big chunk of memory as the one you're allocating in your
program. Notice that if you tried to allocate the same amount of memory
again after you freed it, there's a big chance that you heap will remain
the same and won't increase. Again this will depend on the
implementation of malloc/free.

Hope that helped,
-- 
John V. Shahid <jvshahid@gmail.com>





reply via email to

[Prev in Thread] Current Thread [Next in Thread]