malloc performance issues

Kaia Vintr (vintr@ican.net)
Thu, 15 May 1997 15:30:20 -0400


Someone mentioned recently that there was some concern about malloc
not returning free()'ed memory to the system. I did some small
experimentation with this (kernel 2.1.20, latest linux libc) and as far as I
could determine (based on the numbers reported by "free" and "ps -aux")
free()'ed memory was being returned -- the memory usage of the process
appears to decrease accordingly when large chunks of memory are freed.
My test was to allocate 32 blocks of 1 MB and then free 24 of them chosen
randomly. In every test it appeared that exactly 24 MB was returned to
the system.

Two questions:
1. Am I being fooled? Are "free" and "ps" statistics in some way inaccurate?
(and if so, how is this trick being perpetrated!)

2. If I am not being fooled, how does malloc/free do it? I looked at the source
code for malloc and it doesn't appear to be doing anything fancy, except using
sbrk(-n) when freed blocks are at the end of memory (which wouldn't account for
the behaviour I observed.) Perhaps I looked at the wrong source code -- if so,
could someone tell me which files to look at?

Any source code or documentation references would be appreciated.

- Kaia Vintr [vintr@ican.net]