*BSD News Article 5828


Return to BSD News archive

Path: sserve!manuel!munnari.oz.au!uunet!airs!andrew
From: andrew@airs.com (Andrew Evans)
Newsgroups: comp.unix.bsd
Subject: Re: free() - not working properly
Message-ID: <5453@airs.com>
Date: 30 Sep 92 19:25:25 GMT
References: <9227414.18251@mulga.cs.mu.OZ.AU>
Sender: news@airs.com
Lines: 23

johnp@mundil.cs.mu.OZ.AU (John Steve PETROU) writes:

>If a process mallocs and frees memory durings its normal execution, I find
>that ps -axv reports that the VSZ is always growing.

The UNIXes that I am familiar with all work this way.  When you do a
malloc() request for additional memory, sbrk() is called to increase
the size of the heap if it doesn't contain enough free memory to
fulfill the request.  However, when memory is free()d, sbrk() is not
called to shrink the heap size accordingly.  The free()d memory is
made available for other malloc() requests.

I suspect that this is done for two reasons.  First, unless the
free()d block is at the very top of the heap, it would be impossible
to shrink the heap size without defragmenting all the remaining
allocated memory segments.  This would be very expensive CPU-wise at
each free() operation.  Secondly, if the virtual memory system does
run out of physical RAM and pages out a block of now-unused heap
space, it will not get paged back in until it happens to get
malloc()ed for something else.  So I think that the current scheme is
pretty efficient.
-- 
Andrew Evans (andrew@airs.com) - Infinity Development Systems - Waltham, MA