free() to OS (was: Re: Unix Memory Management : Clearing up a few misconceptions)



On Thu, Mar 18, 1999 at 01:06:36PM -0800, Michael Babcock wrote:
> Guillaume Laurent wrote:
> > First of all, though a few people in this thread have already
> > correctly stated that free() does not release mem to the OS, even more
> > people still have this misconception that, when you free() malloc'ed
> > memory, it is released to the OS. THIS IS FALSE, under most
> > implementations. So, to state things once and for all (yeah right :-) :
> > 
> > UNDER UNIX, WHEN YOU FREE() MEMORY, IT IS NOT RELEASED TO THE OS. THE
> > SIZE OF YOUR PROCESS WON'T EVER SHRINK, IT CAN ONLY INCREASE.
> 
> I just did some tests to confirm that I'm not going insane, and yes
> free() DOES return the memory. I tested X (open and close netscape),
> emacs (load and kill a large buffer) and a trivial program of my own
> that just calls malloc(), then free() a bit later. Even if X and Emacs
> was doing something weird, my test program wasn't.
> 
> According to "ps axm" the SIZE of the process does shrink. Dramatically.
> Is ps not a valid way to determine the memory allocated to a process?
> 
> So maybe this isn't guaranteed by any standard, but its not like I have
> some oddball system here: Debian 2.1 (glibc 2.0).

This is a bit complicated.  It depends on whether the Unix variant
inquestion supports shrinking a process' memory space and whether the
libc in use (which implements malloc() and free() after all) also
supports this.

Every process has one linear chunk of memory (the heap) allocated to it
which it can grow (and probably shrink) using the sbrk syscall (see the
man page).  What it can not do is free one chunk in the middle of this
chunk.  If you allocate something big and free it shortly thereafter,
it will probably be allocated at the end of the heap (by growing it)
and if you free it (without anything being allocated after the end of
that object), the heap could be shrunk again (if OS and libc support
that).

As you see, this only works in special cases.  Advanced programs that
do big allocations can avoid using the heap and instead use mmap() to
allocate memory areas, and these can be freed at run-time, but this
requires some work from the programmer.  There is the mmalloc package
which helps with that.

-- 
    Andreas E. Bombe <andreas.bombe@munich.netsurf.de>
    http://home.pages.de/~andreas.bombe/



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