4

Memory is leaking in a script am working on. On local server it works fine but on the remote server memory leaks. I can’t find the cause of the memory leak.

The error message is:

PHP Fatal error:  Allowed memory size of 67108864 bytes exhausted (tried to allocate 50 bytes) in database.php on line 21

Configuration of local host:

  • PHP 5.4.3
  • MySQL 5.5.24
  • Apache 2.4.2

Configuration of remote host:

  • PHP 5.4.7
  • MySQL 5.5.34
  • Apache 2.2.23

Code triggering the error:

//database.php

class Database {
    public function __construct() {
        $hostname = DB_HOST;
        $dbname = DB_NAME;
        $username = DB_USER;
        $dbpass = DB_PASS;
        try {
            $this->dbh = new PDO("mysql:host=$hostname;dbname=$dbname;charset=utf8", $username, $dbpass);
        } catch (PDOException $e) {
            echo $e->getmessage();
        }
    }

    public function get_data($query, $param = array()) {
            $result = array();
            $stmt = $this->dbh->prepare($query);
            $stmt->execute($param);
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) // this is line 21
            {
                    $result[] = $row;
            }
            return $result;
    }
}
//getusers.php

class getusers {
    public function __construct($dataparam) {
        $this->_db = new Database();
        $this->getmyUsers();
    }

    protected function getmyUsers() {
        $this->myquery = "SELECT name,phone,email,address,company,regdate FROM myusers WHERE user_status=:stat ORDER BY regdate DESC LIMIT 0, 50";
        $this->myparam = array(':stat' => 'on');
        return $this->_db->get_data($this->myquery, $this->myparam);
    }
}

//I ran a test fetching 30 rows

                 $result = array();

        $stmt = $this->dbh->prepare($query);
        $memory_end_one = 0;
        $memory_start_one = memory_get_usage();
        $stmt->execute($param);

        while($row = $stmt->fetch(PDO::FETCH_ASSOC))
        {
        $result[] = $row;
        $memory_end_one = max($memory_end_one, memory_get_usage());
        }
        echo 'initial memory: '.$memory_start_one.'b<br />';
        echo 'Final memory: '. ($memory_end_one - $memory_start_one).'b<br/>';exit();

Results For remote server

  • Initial memory: 216028b
  • Final memory: 178700b

For local server

  • Initial memory: 413664b
  • Final memory: 172632b
andychukse
  • 520
  • 9
  • 19
  • You are fetching records for 50 last registered users with `user_status = 'on'`. You are returning them in an otherwise empty array. Why not use `return $stmt->fetchAll(PDO::FETCH_ASSOC)`? – Palec Jan 20 '14 at 17:08
  • Maybe a related one: http://stackoverflow.com/q/6895098/2157640 – Palec Jan 20 '14 at 17:13
  • @Palec Thanks for the link, but it doesn't solve the problem – andychukse Jan 20 '14 at 17:37
  • When I increase the php memory limit to 128M it works fine. But it executes on local server at 64M. – andychukse Jan 20 '14 at 17:57
  • Is `getusers::getmyUsers` the only place where `Database::get_data` is called? Are you calling `getusers::getmyUsers` repeatedly? For me it is norepro now. See [How to create a Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) in help. – Palec Jan 20 '14 at 17:59
  • 1
    I guess that your script needs lots of memory and your local server is 32b system while your remote server is 64b system. It fits into the limit locally but twice as long pointers are a problem when running on remote server as the limit is exceeded. Please provide memory usage statistics for both machines as in the question I linked to. – Palec Jan 20 '14 at 18:03
  • @Palec I added the test result to the question. On that page Database::get_data is called only once there. – andychukse Jan 20 '14 at 18:35
  • 1
    @Palec I had another query counting the total rows count(fetch(PDO::FETCH_ASSOC)) in the table. Its the one consuming more memory. I used fetchcolumn() for it and it ok now. Thanks for you help. But I would still like to reduce the memory usage seems high. Any suggestions? – andychukse Jan 20 '14 at 18:57
  • Still your question is norepro, as I already wrote. Till you provide a test case, I cannot help. – Palec Jan 20 '14 at 19:26

0 Answers0