通过 pprof 发现,一个 Go 进程实际 in use 内存只占用几百兆,实际物理内存占用了 4、5G
原因很清晰,进程的内存分配器,没有把空闲内存还回系统
挤兑内存可以通过挤兑内存的方式,触发进程归还内存
#include#include #include #include int main() { char *p = NULL; const int MB = 1024 * 1024; while (1) { p = (char *)malloc(100 * MB); memset(p, 0, 100 * MB); sleep(1); } return 0; }
可以添加定时器,定期执行上面代码
当然这种,开发环境可以随便挂个。非最佳实践
主要用于验证
golang 编译选项通过编译时添加GODEBUG=madvdontneed=1也可以达成 golang 程序快速释放内存的目的
细节自己网上搜下,比如 https://blog.csdn.net/qq_39787367/article/details/113872376
参考- https://segmentfault.com/a/1190000022472459
- https://blog.csdn.net/qq_39787367/article/details/113872376



