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
9
  • 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)? Commented Jan 20, 2014 at 17:08
  • Maybe a related one: stackoverflow.com/q/6895098/2157640 Commented Jan 20, 2014 at 17:13
  • @Palec Thanks for the link, but it doesn't solve the problem Commented Jan 20, 2014 at 17:37
  • 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. Commented Jan 20, 2014 at 18:03
  • 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? Commented Jan 20, 2014 at 18:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.