Re: Memory use: returning freed memory to the pool

Adam D. Bradley (
Tue, 28 Jan 1997 11:08:20 -0500 (EST)

> I may be a bit slow witted, but there is something I don't
> understand about memory management: why doesn't the size of my
> process decrease when I free allocated memory ?
> To explain, I've included the program below: this allocates 10 Megs
> (roughly), writes to this memory, to make sure it is actually
> there, then frees it, and goes to sleep for 24 hours.
> When I run this, and monitor the process size using top, I see that
> the size of the process stays at 10 Megs: the freed memory is not
> returned to the operating system.

This is, methinks, a libc problem...malloc() will (when necessary) aquire
a heap of memory w/ a system call, then allocate segments within that
heap; free() marks those sections of the heap "de-allocated", but does not
return it to the OS until the program exits.

There are potential performance hits if free() automatically does a
"hard-release" (correct me if I'm wrong), particularly for
string-intensive and similar applications that are constantly allocating
and freeing substantial amounts of memory.

> - Is there anyway, in which I can force my process to return the
> memory to the operating system, in such a way that Linux can
> recycle this memory ? ( I only need it when starting my process,
> after init I can throw it out).

mallopt() provides a mechanism for "tuning" the behavior of the malloc
function set...of course, my DU4.0 man pages say "it's no longer supported
by us, don't use it", and I can't hit any Linux man pages from here :-P


He feeds on ashes; a deluded mind has led him    Adam Bradley, UNCA Senior
astray, and he cannot deliver himself or say,             Computer Science
"Is there not a lie in my right hand?"   Isaiah 44:20      <><