Funny thing whith malloc/free...

Daniel Hirschberg (
Mon, 11 Nov 1996 11:19:59 -0300 (ChD)

Hi there,

I've got a funny story and a question regarding malloc and free!

First the history... A friend of mine was playing with solaris and AIX
with a little program of his that asks for memory with malloc, sleeps a
little, then frees it and sleeps another time and starts over getting
memory. The sleeps were quite long so he could see the process grow
and shrink (hopefully) using ps.
We found out that the process size reported by ps never went
down... It did not increase when the size asked for was smaller than the
freed space (so free was working OK), but the process never shrunk!

So I told him, "let's try it on my linux, you know, a good os...",
and here comes the funny thing we found:

the process does grow and shrink as expected with many requests
of different sizes, but when it requests about 6 megs it gets them, but
never shrunk again.

Now the question, is this a feature ? am I losing something ?

Best regards,

PS1: the source code is included at the end!

PS2: I tried it on a 1.3.18 kernel, but a friend tried on a 2.0.20 (I
think), with the same results!

PS3: I have not tried it on an intel box so I down't know if it's axp
related or not.


--><-- #include <stdlib.h> #include <stdio.h> #include <malloc.h> main() { int j; char *p; int factor[6] ={1,4,3,12,3,2}; char test[4*1024]; printf("Inicio de prueba\n"); sleep(10); for (j=0; j<6; j++) { p=(char *)calloc(factor[j]*150,sizeof(test)); sprintf(p, "%d*512K de memoria conseguido con calloc", factor[j]); printf("-->%s\n",p); printf("%s\n", p); sleep(5); free(p); printf("memoria liberada\n"); sleep(10); } printf("Fin de prueba\n"); sleep(10); }

To unsubscribe: send e-mail to with
'unsubscribe' as the subject.  Do not send it to

Feedback | Store | News | Support | Product Errata | About Us | Linux Info | Search | JumpWords
No Frames | Show Frames

Copyright © 1995-1997 Red Hat Software. Legal notices