* Michael Sternberg <[EMAIL PROTECTED]> [061003 09:21]: > From man page of "free" on Solaris 8: > > The argument to free() is a pointer to a block previously > allocated by malloc(), calloc(), or realloc(). After free() > is executed, this space is made available for further allo- > cation by the application, though not returned to the sys- > tem. Memory is returned to the system only upon termination > of the application. > > Two questions: > > 1. What the situation in Linux ? Is "free" returns memory to OS ? > 2. If not - what the proper call to "flush" memory back to OS ?
The behaviour of free() depends on the libc that you use, a sane libc will not return the memory to the OS except in special cases. The special case in glibc is when you allocate a very large chunk of memory at once, very large would be in the order of megabytes but for the exact number you'll need to check the glibc docs/code, in this case glibc will mmap an anonymous chunk of memory and when this memory is freed it will immediately be returned to the OS. In the normal case the memory is allocated from a pool of the process. This pool is gathered from the OS with the brk() system call, which allows the process to return memory to the OS but only the top part so if there is an allocation in that part the whole region before it cannot be returned. This memory is reused in future mallocs and glibc will call sbrk/brk only when it doesn't have enough memory in the current heap, thoguh this can happen when the heap is too fragmented and you need to allocate a larger buffer than any existing fragment in the heap. glibc will actually try to return memory to the OS, there is a trim threshold for that which you can set somehow. There seems to be a reference to malloc_trim() function that can ask glibc to release the memory immediately but it is not guaranteed to return anything to the OS, it all depends on the fragmentation. If your application memory usage is not too fragmentary than the unused memory will be swapped out and will not interfere with the system anyway, which is the reason most peoples will simply ignore this whole topic and let the OS and glibc do their magic on their own. Cheers, Baruch ================================================================= To unsubscribe, send mail to [EMAIL PROTECTED] with the word "unsubscribe" in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]
