Note: This web page was automatically created from a PalmOS "pedit32" memo.
malloc and sbrk
Most C runtimes have malloc() implemented overtop of sbrk().
When you request a chunk of memory via malloc(), if there is already
enough memory sbrk()'d to satisfy your malloc() request, then some of
that memory will be parted out to your program as the return value from
malloc().
If not, then more memory will be sbrk()'d to be able to handle the
request.
The thing is, with most C libraries, although you can free malloc()'d
memory easily with free(), there isn't an easy way to un-sbrk() the pool
that's used by malloc.
ISTR that GNU malloc, however, will watch to see if all the malloc()'d
chunks near the end of your process's sbrk() pool for malloc have been
free()'d, and if they have, then it will sbrk() back down. -But-, ISTR
that if you were to malloc() 2 gig, then malloc one byte, then free the
2 gig chunk, I don't think it'll sbrk() back down in that case, because
all the chunks at the end have to be unused to sbrk() back down.
But most malloc()'s don't do that. So if you malloc 2 gig, then free
it, then malloc nothing over a megabyte from there on, you will often
still have at least 2 gig sbrk()'d for future use.