Re: Where's my memory going?!

From: Date: Mon, 28 Sep 2009 21:33:52 +0000
Subject: Re: Where's my memory going?!
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-298395@lists.php.net to get a copy of this message
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, ~Philip
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. <?php class SocketListener extends Socket {
    public function __construct ()
    {
        $this->core = new coreFunctions (false, 'interface');
        parent::__construct ($this->core, $host, $port, $clients);
        $this->readFunction = 'processData';
        $this->startPersistentListener();
    }
    public function processData ($data)
    {
        if ($this->filetype == 'demographics') {
            $demoImporter = new Demographics ();
            $ret = $demoImporter->importFromDataStream ($data);
            $demoImporter->__destruct();
            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->create();
        $this->setOption(SOL_SOCKET, SO_KEEPALIVE, 1);
        $this->bind();
        $this->listen();
        $this->clients = array($this->sock);
        while ($this->isAlive) {
            // Create the list of connected clients
            $this->createClients();
            if ($this->select() < 1) {
                // No clients - go to the next iteration
                continue;
            }
            $this->accept();
            $this->read();
        }
    }
} 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. Thanks! ~Philip

« previous php.general (#298395) next »