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