XmisterIS
07-04-2012, 12:02 PM
My script is consuming ever increasing amounts of memory and I don't know why.
I've whittled it down to the following code, which causes the problem:
$titles = array('a', 'b', 'c', 'd'); //Arbitrary column names.
gc_enable();
foreach ($filenames as $filename) //Array of names of very large data files in CSV format.
{
if ($fp = fopen($filename))
{
echo 'Reading file '.$filename.PHP_EOL;
while (!feof($fp))
{
gc_collect_cycles();
$line = fgetcsv($fp,0, ',', '"');
$assoc = array_combine($titles, $line);
extract($assoc);
$res = mysql_query("insert into mytable (a, b, c, d) values ($a, $b, $c, $d)", $conn);
mysql_free_result($res);
echo (memory_get_usage()/1024/1024).'Mb'.PHP_EOL;
}
fclose($fp);
}
else
echo 'Could not open file '.$filename.PHP_EOL;
}
Now, my (clearly naive) little brain tells me that the line which echos the memory usage should, by and large, report much the same value each time.
BUT ... it doesn't. Itreports an ever-increasing memory usage until the script hits the memory limit.
WTF???? :confused::confused::confused::confused:
I've whittled it down to the following code, which causes the problem:
$titles = array('a', 'b', 'c', 'd'); //Arbitrary column names.
gc_enable();
foreach ($filenames as $filename) //Array of names of very large data files in CSV format.
{
if ($fp = fopen($filename))
{
echo 'Reading file '.$filename.PHP_EOL;
while (!feof($fp))
{
gc_collect_cycles();
$line = fgetcsv($fp,0, ',', '"');
$assoc = array_combine($titles, $line);
extract($assoc);
$res = mysql_query("insert into mytable (a, b, c, d) values ($a, $b, $c, $d)", $conn);
mysql_free_result($res);
echo (memory_get_usage()/1024/1024).'Mb'.PHP_EOL;
}
fclose($fp);
}
else
echo 'Could not open file '.$filename.PHP_EOL;
}
Now, my (clearly naive) little brain tells me that the line which echos the memory usage should, by and large, report much the same value each time.
BUT ... it doesn't. Itreports an ever-increasing memory usage until the script hits the memory limit.
WTF???? :confused::confused::confused::confused: