From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,2c626cf8713c3d7f X-Google-Attributes: gidfee84,public From: Al Simons Subject: Re: is free() lazy ? Date: 1997/10/01 Message-ID: <3432538C.220D@zk3.dec.com>#1/1 X-Deja-AN: 277039805 References: Reply-To: simons@zk3.dec.com Organization: DIGITAL UNIX Development Environment Newsgroups: comp.unix.osf.osf1 Pierre Monestie wrote: > > Hi, > If in a c program I allocate a certain amount of memory > and then I fred it, when I do a ps on my program I realize > that the prog is taking (RSS value) the amount of memory > I previously allocated. I know this memory is free but the > system don't get it back. Is there a way (A kernel config?) to really > "free" this memory. > Nb : if i run my program on a linux pc, the memory is effectively freed. There are two items getting in your way. The first is that free() is a little bit lazy. It delays freeing a chunk of memory until the next call to free. This is to accommodate a large number of buggy applications which call realloc after calling free on the chunk of memory. But that is not the primary cause of the problem you are seeing. DIGITAL UNIX's kernel has the default behavior that memory allocated by sbrk() is not actually removed from the process's virtual address space with a call to sbrk() with a negative value. This is a performance driven decision. Since it is not removed from the VAS, your RSS and swapfile requirements track the peak usage of your application. There is a way to override this: use dbx -k to change the value of kernel variable old_obreak to zero. This will cause the kernel to respect sbrk() calls with a negative value. Note that once the kernel variable is set to zero, the conditions under which memory is returned to the system (removed from the process address space) are controlled by three malloc tuning parameters: __noshrink, __minshrink and __minshrinkfactor. See the malloc man page for more information on these parameters and their use. Al Simons DIGITAL UNIX Development Environment Group simons@zk3.dec.com