mmap小内存,当munmap时,glibc是否会将映射内存释放给操作系统?
在多线程服务器程序中,我发现常驻内存从1G增加到20G,并且没有下降。我用内存工具检查过,程序没有内存泄漏。 所以我想也许 glibc 保留释放的内存,没有释放到操作系统。我用 mmap 替换 alloc (一般来说,每个 mmap 会申请 65-100 k 内存),期望当我调用 munmap 时,映射的内存将返回到操作系统,但我观察程序运行 1 天后仍然占用 20G 内存? 所以我想知道:munmap肯定会将映射内存返回给操作系统吗? 和其他建议? 谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,对于小分配(<128k,我记得),glibc 通常会使用 sbrk 并维护自己的空闲列表。对于较大的分配,它将使用
mmap()
和munmap()
。munmap
肯定会将内存归还给系统。这是一个系统调用; glibc 只是将其传递给内核。假设这是 Linux,您可以通过编写一些测试代码并执行
cat /proc/PID/maps
来验证 mmap 的行为,其中PID
是您进程的进程 ID。它将打印内核为进程维护的每个虚拟内存区域(VMA)的行;本质上,每个 mmap() 对应一个。不过,即使 glibc 不将内存归还给系统,它也会通过自己的内部空闲列表进行回收。鉴于此,再加上使用 mmap()/munmap() 没有改变任何内容,您是否想到过您的程序可能存在内存泄漏?
Yes, for small allocations (< 128k, as I recall) glibc will generally use
sbrk
and maintain its own free list. For larger allocations, it will usemmap()
andmunmap()
.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
wherePID
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?