php扩展如何做内存检查

一、重新编译php并加上 --enable-debug 打开debug模式,如果php版本在5.2以下还要加上--disable-zend-memory-manager

./configure --enable-debug 
make
make install

 

二、对自己编写的扩展重新编译生成debug版本的so ,注意两个版本的目录差别

debug-non-zts-20060613 
no-debug-non-zts-20060613

 

三、修改php.ini把extension_dir目录设置为debug的so目录。不要拿之前编译过的php源码目录直接编译,不然可能加载不了扩展,php -m会出现如下错误:

Module compiled with module API=20060613, debug=1, thread-safety=0 //已经是debug的了
PHP    compiled with module API=20060613, debug=0, thread-safety=0  //没有打开debug
These options need to match in Unknown on line 0
接着确认php.ini里report_memleaks被设置为On这样执行php会直接报出内存泄漏的点,这个选向只有debug模式并且error_reporting包含E_WARNING时生效。
 
四、使用php -f 运行文件 或者用phpunit 或者pear run-tests tests/*.phpt 都可以看到内存泄漏(感谢moxie的无私帮助)。如
Stream of type 'tcp_socket' 0x8f94f98 (path:(null)) was not closed
 
五、如果上面测试通过,可以使用valgrind 进行进一步测试了
 

1.禁止zend内存管理

Zend Engine uses its own routines to optimize memory management, but because of this valgrind cannot see most of the memory issues. You must disable Zend memory manager before running PHP with valgrind. In order to do this you need to set USE_ZEND_ALLOC environment variable to 0.

Use

export USE_ZEND_ALLOC=0

or

setenv USE_ZEND_ALLOC 0

(the syntax depends on what your shell supports).

2.通过valgrind运行PHP CLI 或 PHP CGI 

To generate the valgrind log using PHP CLI/CGI, you need to execute the following command:

 
 valgrind --tool=memcheck --num-callers=30 --log-file=php.log /path/to/php-cli script.php
 

This should put the log into php.log file in the current working directory.

3.通过valgrind运行PHP Apache module 

If you compiled PHP and Apache statically, make sure the Apache binary is not stripped after make install, otherwise you lose the required debug info. To check it, run file /path/to/httpd, it should output something like this (notice "not stripped"):

 
 # file /usr/local/apache2/bin/httpd
 /usr/local/apache2/bin/httpd: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), for GNU/Linux 2.6.4, dynamically linked (uses shared libs), not stripped
 

To generate the valgrind log using PHP as Apache module, you need to run the Apache itself under valgrind:

 
 valgrind --tool=memcheck --num-callers=30 --log-file=apache.log /usr/local/apache/bin/httpd -X
 

This should put all the memory errors into into apache.log file.

 

参考:

https://bugs.php.net/bugs-getting-valgrind-log.php

http://qa.php.net/phpt_details.php

http://www.ibm.com/developerworks/cn/opensource/os-cn-php-autotest/index.html

PHP扩展开发手记

http://blog.zoeey.org/2012/05/07/note-of-php-extension-dev/

Tags: 内存 , extension , valgrind , php

上一篇: smtpmail更新0.3.1   下一篇: smtpmail更新0.3.2

你问我答

  1. #1 头像 keminar 2012-11-11 19:10:35
    增加参数
    valgrind --tool=memcheck   --leak-check=full  --show-reachable=yes  /usr/local/bin/php script.php

提交疑问

回顶部