On Sep 28, 2009, at 4:18 PM, Jim Lucas wrote:

Philip Thompson wrote:
Hi all.

I have a script that opens a socket, creates a persistent mysql
connection, and loops to receive data. When the amount of specified data has been received, it calls a class which processes the data and inserts
it into the database. Each iteration, I unset/destruct that class I
call. However, the script keeps going up in memory and eventually runs out, causing a fatal error. Any thoughts on where to start to see where
I'm losing my memory?

Thanks in advance,

We cannot tell you anything without see an example of what your script is doing.

Let us see some code!

I was wondering if you were gonna ask that! Umm... I don't know if I can show you the code b/c it's not as trivial as I explained it above. I'll tell you what, I give you something to chew on and you let me know if it's enough.

class SocketListener extends Socket
    public function __construct ()
        $this->core = new coreFunctions (false, 'interface');
        parent::__construct ($this->core, $host, $port, $clients);
        $this->readFunction = 'processData';

    public function processData ($data)
        if ($this->filetype == 'demographics') {
            $demoImporter = new Demographics ();
            $ret = $demoImporter->importFromDataStream ($data);
            unset ($demoImporter);
echo "Memory usage: " . number_format (memory_get_usage ()) . "\n";
            return $ret;

class Socket
    public function startPersistentListener ()
        // Create a persistent listening socket connection
        $this->setOption(SOL_SOCKET, SO_KEEPALIVE, 1);
        $this->clients = array($this->sock);

        while ($this->isAlive) {
            // Create the list of connected clients

            if ($this->select() < 1) {
                // No clients - go to the next iteration


set_time_limit (0);
new SocketListener ();

I stripped out a lot of code, but this is the basics. I have a listener class that extends the socket class. When the socket class receives some data, it calls the processData() method - which creates a new instance of the Demographics class. It destroys the instance after it's finished. The line where I echo the memory usage indicates that the memory continues to grow until it just explodes.

There's a lot more going on behind the scenes that *could* cause the memory leak, but hopefully this will give you somewhere to go.


PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to