Hello! 2013/6/12 胡鹏: > 最近开发了一个模块,发现有轻微的内存泄露,光看代码看不出来,压测也要压很久(几天)才有效果, > 想找一个内存泄露检测工具试一试,不知道一般用什么工具?看了一些内存检测工具,好像基本都是检测 > 执行一次的程序的,像nginx这样守护进程的程序怎么做内存泄露检测? 在 nginx.conf 里面配置 worker_processes 1; daemon on; master_process off; 然后用 valgrind 启动 nginx,比如: valgrind --tool=memcheck --leak-check=full -q \ --show-possibly-lost=no nginx -c /path/to/nginx.conf 然后照常请求 nginx 监听的端口,完事后给 valgrind/nginx 进程发 QUIT 信号让其正常退出就可以了。valgrind 的输出里会指示各种内存问题(如果有的话)。 我的 Test::Nginx 测试台集成了包括 valgrind 在内的许多工具: http://search.cpan.org/perldoc?Test::Nginx#valgrind_integration 我维护的十几个 nginx 模块的测试集都可以直接以 valgrind 模式运行,只需指定 TEST_NGINX_USE_VALGRIND=1 这个环境变量就可以了。见我的 Amazon EC2 测试集群生成的在线测试报告: http://qa.openresty.org/ 特别需要指出的是,nginx 自己的内存池会干扰 valgrind 这样的内存检测工具发现许多问题(比如不少越界写和越界读以及小内存块的 double free 问题 ),建议总是在测试时给你的 nginx 打上 no-pool 补丁: https://github.com/shrimp/no-pool-nginx 这是我们用线上频繁崩溃换来的血的教训 :) 当然,Valgrind 并不能发现所有的内存问题,所以我才在 Nginx Systemtap Toolkit 里面编写了一系列工具用来诊断其他一些种类的问题: https://github.com/agentzh/nginx-systemtap-toolkit 比如其中的 ngx-shm, ngx-leaked-pools, ngx-cycle-pool, ngx-active-reqs 这些工具。 如果使用一些脚本语言 VM,比如 LuaJIT VM,则诊断其 GC 相关的问题又需要其他专门的工具了 :) 同时抄送给 openresty 中文邮件列表:https://groups.google.com/group/openresty Best regards, -agentzh
Hello! 2013/6/13 agentzh: > 在 nginx.conf 里面配置 > > worker_processes 1; > daemon on; > master_process off; > 不好意思,这里有一处笔误,应当是 daemon off; Best regards, -agentzh