Forum Moderators: coopster

Message Too Old, No Replies

Memory Management Problem

Problem with memory management

         

JSwoff

12:13 am on Apr 30, 2008 (gmt 0)

10+ Year Member



Hi all, I am having some memory Management Problems. The Below Scrip Collects a list of 4,000 Files and attempts to upload them. But with each iteration, even though I unset almost all values except for $filelist, the memory usage keeps growing and growing and growing until about the 2000th iteration where it again attempts to load a new XML stream.

Can anyone please look through this and give me some pointers on what I can possibly do to fix this? I am going crazy right now!

Oh and 1st time poster, long time lurker :P


<?PHP
require_once('mysql_connect.php');

$filelist = explode("\n",`find XML -name '*.xml'`);

foreach ($filelist as $location) {

//$location = 'XML/YAMAHA_2008_22226_03172008/YAMAHA_2008_108724.xml';
if (strlen(trim($location)) !== 0 ){
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname,$conn);

echo '<h3>I AM LOCATION: ' . $location . '</h3><br>';

$file = file_get_contents($location);
$xml2 = new SimpleXMLElement($file);

foreach($xml2->model_info->Children() as $node){
$string = $node->getName();
$query = sprintf("INSERT INTO elements (model,file) VALUES ('%s','%s')",
mysql_real_escape_string($string),
mysql_real_escape_string($location));
mysql_query($query,$conn) or die(mysql_errno);
}

$mfg = $xml2->model_info->make;
$model = $xml2->model_info->productname ;
$query1 = sprintf("INSERT INTO models (mfg,model,file) VALUES ('%s','%s','%s')",mysql_real_escape_string($mfg),mysql_real_escape_string($model),mysql_real_escape_string($location));

echo '<b>Query 1: </b>' . $query1 . '<br>';
mysql_query($query1,$conn) or die(mysql_errno);

echo '<b>Memory In Use: </b>' . memory_get_usage() . '<br>' . "\n";
echo '<b>Peak Usage: </b>' . memory_get_peak_usage(). '<BR>' . "\n";

echo 'I= ' . $i . '<br>';
$i++;

}
echo 'A= ' .$a . '<BR>';
$a++;

$xml2 = &$GLOBALS[$name];
unset ($GLOBALS[$name]);
unset($location);
unset($conn);
unset($file);
unset($node);
unset($query);
unset($query1);
unset($mfg);
unset($model);

}

?>

RonPK

10:28 am on Apr 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try moving the lines with mysql_connect() and mysql_select_db() out of the outer loop. No need to set up a new connection in every iteration.

I guess that causes memory problems as the connections are not closed.

JSwoff

5:15 pm on Apr 30, 2008 (gmt 0)

10+ Year Member



I was actually originally doing it just from the mysql_connect.php, when I first noticed the problem. I really have no idea what to do, it seems as if the contents of the file() or the $xml2 Stream are not relinquishing memory upon being unset.

And from my knowledge there is no way to force garbage collection.

RonPK

11:02 am on May 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There turns out to be a SimpleXML memory issue (bug #41562 [bugs.php.net]), which has been fixed in 5.2.6.