Reputation: 571
in multi-thread server program, I found the resident memory increase from 1G to 20G, and didn't go down. I have check with memory tools, and the program has no memory leak. so I think maybe glibc hold freed memory, didn't release to OS. I replace alloc by mmap (in general, every mmap will apply 65-100 k memory), expect that when I call munmap, the mapped memory will be return to OS, but I observe the program still occupy 20G memory after running 1 day ? so I want to know : munmap will definitely return mapped memory to OS ? and other advice ? thank you.
Upvotes: 2
Views: 1702
Reputation: 71525
Yes, for small allocations (< 128k, as I recall) glibc will generally use sbrk
and maintain its own free list. For larger allocations, it will use mmap()
and munmap()
.
munmap
will definitely return the memory to the system. It is a system call; glibc just passes it through to the kernel.
Assuming this is Linux, you can verify mmap's behavior for yourself by writing some test code and doing cat /proc/PID/maps
where PID
is the process ID of your process. It will print for line for each virtual memory area (VMA) that the kernel is maintaining for your process; essentially, one for each mmap().
However, even if glibc does not return the memory to the system, it will recycle it via its own internal free list. Given that, plus the fact that using mmap()/munmap() did not change anything, has it occurred to you that perhaps your program has a memory leak?
Upvotes: 2