Search code examples
phpmemory-leaksdaemon

php daemon possible memory leak


i've written a daemon in php and want to make sure it doesn't leak memory, as it'll be running 24/7.

even in its simplest form memory_get_peak_usage for a daemon will report that the script consumes more memory for each cycle. memory_get_usage on the other hand will not grow.

the question is: should i worry? i've stripped down the daemon to the bare basics but this is still happening. any thoughts?

#!/usr/bin/php -q

<?php
require_once "System/Daemon.php";
System_Daemon::setOption("appName", "smsd");
System_Daemon::start();
while(!System_Daemon::isDying()){
 System_Daemon::info("debug: memory_get_peak_usage: ".memory_get_peak_usage());
 System_Daemon::info("debug: memory_get_usage: ".memory_get_usage());
 System_Daemon::iterate(2);
}

FINAL NOTE + CONCLUSION: i ended up writing my own daemon wrapper, not using pear's system_daemon. regardless of how i tweaked this library i could not stop it from leaking memory. hope this helps someone else.

FINAL NOTE + CONCLUSION 2: my script has been in production for over a week and is still not leaking 1 bytes of memory. so - writing a daemon in php actually seems to be ok, as long as you're very careful about its memory consumtion.


Solution

  • I got the same problem. Maybe the best idea is to report new bug at PEAR

    BTW, code like that doesn't show that memleak:

    #!/usr/bin/php -q
    
    <?php
    require_once "System/Daemon.php";
    System_Daemon::setOption("appName", "smsd");
    System_Daemon::start();
    while(!System_Daemon::isDying()) {
     print ("debug: memory_get_peak_usage: ".memory_get_peak_usage()."\n");
     print ("debug: memory_get_usage: ".memory_get_usage()."\n\n");
     System_Daemon::iterate(2);
    
    }
    

    Look's like System_Daemon::info() is a problem.