Skip to content

Instantly share code, notes, and snippets.

@yewton
Created March 30, 2012 02:24
Show Gist options
  • Save yewton/2245904 to your computer and use it in GitHub Desktop.
Save yewton/2245904 to your computer and use it in GitHub Desktop.
PDO::FETCH_INTO leaks memory; PHP 5.3.10, MySQL 5.0.77
<?php
class A {
public $m;
public function __construct($pdo) {
$sql = 'select 1 as m';
$stmt = $pdo->prepare($sql);
$stmt->setFetchMode(PDO::FETCH_INTO, $this);
$stmt->execute();
}
}
class B {
public $m;
public function __construct($pdo) {
$sql = 'select 1 as m';
$stmt = $pdo->prepare($sql);
$stmt->execute();
$res = $stmt->fetch(PDO::FETCH_ASSOC);
foreach($res as $k => $v) $this->$k = $v;
}
}
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'root', '');
echo "Start mem:".memory_get_peak_usage()."\n";
echo "-- fetch assoc --\n";
for($i=0; $i<100000; $i++) {
$test = new B($pdo);
unset($test);
echo "mem: ".memory_get_peak_usage()."\r";
}
echo "\n";
echo "-- fetch into this --\n";
for($i=0; $i<100000; $i++) {
$test = new A($pdo);
unset($test);
echo "mem: ".memory_get_peak_usage()."\r";
}
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment