How to track down a memory leak?

I think one of my plug-ins may be leaking memory (one of the plug-ins I coded) in production use. Any idea/suggestions/tips how one goes about tracking down a memory leak in a plug-in? Running TK Linux as host.

Thanks!

P.S. Can't turn the plug-ins off because it's a production environment and the content would display very, very poorly if I did. Argh.

  • Timothy Bowers
    • Chief Pigeon

    Hey David.

    If turning things off is not an option then you will need to replicate that install so you can get dirty with it all and give it a good testing.

    Here is a Bash script to mirror WordPress MU installation into a sandbox:

    http://pp19dd.com/2011/01/bash-script-to-mirror-wordpress-mu-installation-into-a-sandbox/

    Not for the faint hearted, backups galore perhaps needed. As the author Warns!:

    WARNING: read over this script before you run. Seriously. Did you notice there are rm -rf statements in it?

    To track those memory leaks you might like to consider:

    http://php.net/manual/en/function.memory-get-usage.php

    http://www.xdebug.org/ and then: http://xdebug.org/docs/execution_trace

    Or if you are running PHP 5.3+ Then there is a garbage collector:

    http://www.php.net/manual/en/features.gc.php

    I hope this of this helps you out. :slight_smile:

    Let us know how you get on David.

    Take care.

  • David
    • The Crimson Coder

    Thanks. I've pretty much ascertained it's somewhere in the Apache/PHP stack, because one of my Apache processes grows over time, Since I rebooted this morning, that process is at 782MB. But I almost can't see how it could be one of my plug-ins because WordPress isn't reentrant. It just loads per page, and then the session ends, and PHP frees up memory. I'm not running a daemon or anything that runs continuously.

    But the problem is, I added two additional WPMU sites to the server and I noticed it starting to eat more memory. My guess is it's my code, but it's not necessarily the case. What's weird is that it keeps using more system memory, growing into virtual memory over time, and yet PHP should release that RAM after each page.

    Any ideas how PHP could be holding RAM between sessions when I'm not running anything daemon-like? Or is there something secretly in WP that is daemon-like (continuously running, long scripts) that I'm not aware of?

    Thanks!

  • Timothy Bowers
    • Chief Pigeon

    I know when I've had issues before I've found its something related to WP-Cron, often when the sites are bombarding by spam.

    A few years ago I had an issue with a server going down around the same time every morning. You could see it building up. The issue was a mixture of things, upgrading PHP and WP helped some of that. A plugin was also causing an issue as well, but I forget which one it was.

    What is currently being processed, which files are taking the most on your server?

  • SooBahkDo
    • Syntax Hero

    Hi David,

    This may not relate at all to your environment, but we just solved a pesky escalting memory use issue that would take down the server on a cycle of about 3-5 days. We discovered a bug in Cpanel with its handling of Fasctcgi process killing that was allowing the numbr of active processes to grow forever without killing any off even though settings were in place to kill them after a certain time period.

    We previously had no trouble even though the bug was there all along until we activated and began using the members and blogs directory plugins. Only then did our server start throwing 500 errors, etc. Turns out it was coincidental to their use, but those plugins helped us discover that the root problem was hiding in wait all along.

    The thread with bug reports & workaround:
    http://forums.cpanel.net/f5/apache-graceful-restarts-fastcgi-mod_fcgid-cant-lock-process-table-pid-151373.html

    Best of luck,
    Phil D

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.