_php tutorial for detecting PHP extended memory leaks with Valgrind

Source: Internet
Author: User
Tags apache php php cli valgrind
Original: https://bugs.php.net/bugs-getting-valgrind-log.php

Premise

1. When compiling PHP, you must bring it on --enable-debug选项 .

2, disable the memory management of PHP.

Disable Zend MM

Zend Virtual machines use their own programs to optimize memory management, so Valgrind cannot detect most of the memory problems. Before executing PHP with Valgrind, you must disable Zend's own memory manager. Disabled by setting the environment variable USE_ZEND_ALLOC to 0.

Export use_zend_alloc=0

Or

Setenv Use_zend_alloc 0

The above method is applicable to php5.2 and above versions. PHP before 5.2 needs to be compiled with an --disable-zend-memory-manager option.

Using shared extensions

In order to correctly display the extension memory stack in Valgrind, you need to set:

Export Zend_dont_unload_modules=1

Or

Setenv Zend_dont_unload_modules 1

This setting is used for PHP 5.3.11 and later versions.

Editor's note: For example, if not set ZEND_DONT_UNLOAD_MODULES , Valgrind may report

$ valgrind--leak-check=full--show-reachable=yes php test.php
...
==25829== 8 bytes in 1 blocks is indirectly lost in loss record 2 of 21
==25829== at 0x4c25e84:???
==25829== by 0XCE440DC:???
==25829== by 0xce44316:???
==25829== by 0xce44368:???
==25829== by 0xcbee55f:???
==25829== by 0xcbd3f87:???
==25829== by 0x949a85:zend_activate_modules (zend_api.c:2285)
==25829== by 0x8b5ebc:php_request_startup (main.c:1491)
==25829== by 0xa84f7b:main (php_cli.c:1356)
...

If set ZEND_DONT_UNLOAD_MODULES , the following is displayed

$ valgrind--leak-check=full--show-reachable=yes php test.php
...
==25824== 8 bytes in 1 blocks is still reachable in loss record 2 of 30
==25824== at 0x4c25e84:calloc (in/usr/lib/valgrind/vgpreload_memcheck.so)
==25824== by 0xce440dc:event_base_priority_init (in/usr/lib/libevent-1.4.so.2.1.3)
==25824== by 0xce44316:event_base_new (in/usr/lib/libevent-1.4.so.2.1.3)
==25824== by 0xce44368:event_init (in/usr/lib/libevent-1.4.so.2.1.3)
==25824== by 0xcbee55f:zm_activate_http_request_pool (http_request_pool_api.c:58)
==25824== by 0xcbd3f87:zm_activate_http (http.c:373)
==25824== by 0x949a85:zend_activate_modules (zend_api.c:2285)
==25824== by 0x8b5ebc:php_request_startup (main.c:1491)
==25824== by 0xa84f7b:main (php_cli.c:1356)
...

Use Cli,web server built-in or CGI to execute PHP

In order for PHP cli/cgi to generate Valgrind logs, you need to do this with the following command:

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

This will output the log to the Php.log file in the current directory.

If you want to detect PHP built into Web server, you need to use the appropriate-s and-t parameters for the CLI executable file. It is then executed through the browser request and then the Valgrind error in Php.log.

Execute PHP Apache module via Valgrind

If you are statically compiling PHP and Apache, then you need to make sure that the Apache bin is not separated after making install, otherwise it will lose the required debugging information. The detection is as follows, execution/path/to/httpd,这样会输出一些东西(例如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

If you want to generate Valgrind test reports for Apache PHP mods, you will need to run Apache under Valgrind:

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

Accessed through a browser request, all memory errors are output to apache.log.

http://www.bkjia.com/PHPjc/440136.html www.bkjia.com true http://www.bkjia.com/PHPjc/440136.html techarticle Original: https://bugs.php.net/bugs-getting-valgrind-log.php premise 1, when compiling PHP, you must bring the--ENABLE-DEBUG option. 2, disable the memory management of PHP. Disable Zend MM Z ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.