Memory use: returning freed memory to the pool

Tim Timmerman (
Tue, 28 Jan 1997 12:29:55 +0100

(You may see this one twice.. I accidentally sent it to
owner-linux-kernel .. sorry)

Not sure whether this is the appropriate forum for this question,
but the issue is kernel related, I think:

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.

Running multiple processes simply means that the system runs out of
swap space... You'd expect memory to be recycled.

Couple of questions:
- Why isn't it returned to the operating system ? Is there some
technical/security reason for this ?

- 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).


#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

#define MEMORY 10000000
| Function prototypes |
void main ( void)
char *mem_ptr;
int i;
time_t now;

int pid = getpid();
now = time( NULL);

printf( "pid %d, started at %s\n", pid, ctime(&now));

mem_ptr = malloc( MEMORY); /* Malloc a lot of memory */

sleep(60); /* sleep for one minute */
now = time(NULL);
printf( "pid %d, polluting memory at %s\n", pid, ctime(&now));

for ( i = 0; i < MEMORY; i++)
mem_ptr[i] = time(NULL);

now = time(NULL);
printf( "pid %d, polluted memory at %s\n", pid, ctime(&now));

sleep( 60);
now = time(NULL);
printf( "pid %d, freeing memory at %s\n", pid, ctime( &now));

free( mem_ptr);
sleep( 24 * 3600);
now = time( NULL);
printf( "pid %d, exit at %s\n", pid, ctime( &now));


---------------------------------------------------------------------------- Tel: (Int+031)-(0)40-2580613 Voodoo Programmer/Keeper of the Rubber Chicken
He who laughs last, is not very quickwitted

---------------------------------------------------------------------------- Tel: (Int+031)-(0)40-2580613 Voodoo Programmer/Keeper of the Rubber Chicken
Life takes its toll. Have exact change ready. . . .