Seiden Group
Modern Development & Open Source for IBM i
  • Link to LinkedIn
  • Link to Mail
  • Home
  • Seiden PHP+
    • Seiden PHP+
    • Install & Learn
    • SmartSupport
    • PHP Migrations & Upgrades
    • Success Stories
    • Free PHP Assessment
    • Documentation
    • What’s New (Changelog)
  • IBM i Services
    • Development
    • Training & Mentoring
    • Open Source Setup & Upgrades
    • SSL/TLS Install & Learn
    • Performance
  • Support
    • Open Source & PHP
    • VS Code for i
    • Developer Support
    • Support Success Stories
  • VS Code for i
    • Support
    • Training
    • Code for i Resource Guide
    • Getting Started Videos
    • Code for IBM i Fridays
  • Free Stuff
    • IBM i Strategy & Tips
    • PHP Upgrade Assessment
    • CNX Valence Assessment
    • VS Code for IBM i Resources
    • Code for IBM i Fridays
    • QCachegrind Download
    • PHP Toolkit for IBM i Resources
    • Qshell on i Library
  • Blog
  • About
    • About Our Team
    • About Alan Seiden
    • Speakers & Sessions
    • In the News
  • Contact
  • 201.447.2437
  • Search
  • Menu Menu

Seiden Group Blog

Detecting Memory Leaks in PHP Extensions During Development

February 3, 2021/4 Comments/in PHP /by Calvin Buckley

memory leakWhen a PHP extension has a memory leak, mysterious crashes can result, forcing users to restart the web server for relief.

As official maintainers of the ibm_db2 and PDO_IBM extensions, we’ve been on a quest to find and eliminate any memory leaks from these popular PHP modules.

With such a comprehensive goal, we needed a strategy. For extensions that have comprehensive test suites we decided that, in addition to reviewing the usual regression tests, why not also use the tests to detect leaks?

What follows is a technical look at how we do it.

Building PHP in debug mode

A PHP executable built for debug mode will report leaks found at the runtime level, including extensions. You can enable the debug mode by passing --enable-debug to the configure script when building PHP. It prints out a message when it does find a leak, and the location of the offending allocation:

1
2
3
[Mon Dec 21 12:43:55 2020]  Script:  '/home/calvin/src/pecl-database-pdo_ibm/tests/fvt_66620_V6_stored_proc_io_int.php'
/QOpenSys/pkgs/include/php/Zend/zend_string.h(225) :  Freeing 0x0700000000001a00 (48 bytes), script=/home/calvin/src/pecl-database-pdo_ibm/tests/fvt_66620_V6_stored_proc_io_int.php
=== Total 1 memory leaks detected ===

How memory leaks occur

Everything allocated must be freed, and freed only once. Thus, you need to keep track of where you allocate memory, and how long that memory needs to last. Otherwise, the data leaks and becomes unaccounted for even if all references to it disappear. Once you find the line of code that the leak occurred in, you can try to follow the code backwards (how it got there), or use a debugger and place a breakpoint. With that, then you can think about where it should be freed (the lifetime of the data).

If it finds multiple leaks, but one is a duplicate of another, debug mode will also report the duplication. Tests will often test the same thing in different ways, so duplicates can happen a lot.

1
2
3
4
[Wed Dec 16 11:01:27 2020]  Script:  '/home/calvin/src/pecl-database-pdo_ibm/tests/fvt_017c_SelectLOBs.php'
/home/calvin/src/pecl-database-pdo_ibm/ibm_statement.c(149) :  Freeing 0x070000000009b050 (36 bytes), script=/home/calvin/src/pecl-database-pdo_ibm/tests/fvt_017c_SelectLOBs.php
Last leak repeated 1 time
=== Total 2 memory leaks detected ===

Your test suite will tell you

How does your test suite know there was a memory leak?

PHP’s test suite compares the output of the test with known good output. Optionally, tests can use format strings or regular expressions for fancier comparisons. If actual output does not match expected output, it’ll report the problematic test.

Because PHP’s leak reports append text to the output, the reports will be extraneous output that the test didn’t expect. This will cause the test to fail. Thus, if you have a test suite that passes, you can identify what tests are causing leaks. And because the tests are minimal viable examples containing very few lines of code, it’ll be easier to trace the source of the leak, too.

Tags: AS/400, db2_connect, i5, ibm_db2, IBMi, iSeries, pdo_ibm, PHP, Zend
Share this entry
  • Share on Facebook
  • Share on X
  • Share on Pinterest
  • Share on LinkedIn
  • Share on Tumblr
  • Share on Reddit
  • Share by Mail
https://www.seidengroup.com/wp-content/uploads/2017/03/SeidenLogo-180.png 0 0 Calvin Buckley https://www.seidengroup.com/wp-content/uploads/2017/03/SeidenLogo-180.png Calvin Buckley2021-02-03 09:32:422021-02-02 17:35:09Detecting Memory Leaks in PHP Extensions During Development
Calvin Buckley

About Calvin Buckley

Recognized as an IBM Champion and a 2020 Fresh Face of IBM i, Calvin exhibits remarkable talent for expanding the use of IBM i through open ...Read More

4 replies
  1. Ovais Mirza
    Ovais Mirza says:
    January 17, 2025 at 7:08 am

    This is a fantastic guide on detecting memory leaks in PHP extensions! The step-by-step process of enabling debug mode and using test suites to identify memory issues is very practical.

    By combining this with a clear understanding of memory allocation and freeing, developers can significantly reduce the risk of crashes caused by memory leaks. Great resource for PHP extension developers!

    Reply
  2. Brij Bhushan
    Brij Bhushan says:
    March 27, 2021 at 7:54 am

    Hello Calvin,
    It is a great sharing…I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article.

    Reply
  3. Jack Woehr
    Jack Woehr says:
    February 6, 2021 at 11:07 am

    Those of us who know Calvin and his work feel that Seiden’s cut of PHP is safe and in good hands of this very creative and diligent codemeister!

    Reply
    • Alan Seiden
      Alan Seiden says:
      March 4, 2021 at 7:20 am

      Thank you, Jack! “Codemeister” should be Calvin’s new job title!

      Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

SUBSCRIBE
Open Thinking
Monthly IBM i Strategy & Tips
  • This field is for validation purposes and should be left unchanged.

IBM i Development

  • RPG, COBOL, SQL, Node,
    PHP, Python, Valance, etc.
  • Modernization. Integration.
  • Web and API Solutions
  • Legacy Maintenance
    ...
LET'S GET IT DONE!

Recent Posts

  • Mitigating the eMudhra Intermediate CA Compromise with IBM i
  • Critical Security Update Available in Seiden PHP+ Server
  • Moving to IBM i 7.6? An SDA Alternative with VS Code and IBM Bob
  • What’s New with Mapepire Database Connector for IBM i
  • Do You Need IBM i Observability?
  • PHP 8.6 is Coming Soon to IBM i
  • Safer npm installation is on the way for Node.js (and all JavaScript)
  • Access Modern Security for IBM i and Connected Web Environments
  • IBM i: A Natural Platform for Agentic AI
  • PHP on IBM i in 2026: The Modernization Engine

SEIDEN GROUP: Modern Development & Open Source for IBM i

Home   |   Seiden PHP+   |   IBM i Services  |   Support   |   VS Code for i   |   Free Stuff   |   Blog  |   Privacy Policy  |   Contact         201.447.2437

© 2026 Seiden Group, LLC
  • Link to LinkedIn
  • Link to Mail
Link to: Free QCachegrind Enables Application Profiling for PHP Developers Link to: Free QCachegrind Enables Application Profiling for PHP Developers Free QCachegrind Enables Application Profiling for PHP Developers Link to: Node.js and IBM i – Getting Started Guide Link to: Node.js and IBM i – Getting Started Guide Node.js and IBM i – Getting Started Guide
Scroll to top Scroll to top Scroll to top