Log in

View Full Version : php memory



kosi
08-24-2007, 06:39 AM
So I have this really big, presumably memory intensive script. It always worked before, but now it's giving me this error:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 132877 bytes) in [pathtofile] on line 212

There's nothing spectacular about that particular line; i think it may just be that that's where it was when it ran out of memory. Anyhow, it's a form script with itself as the action, and it interacts heavily with mysql. I've tried using mysql_free_result(), but that doesn't seem to help so much. Any other suggestions for freeing up memory space?

Twey
08-24-2007, 08:05 AM
Process each row at a time rather than trying to fit the whole thing into memory at once? I'd have to see the code.

kosi
08-24-2007, 08:21 AM
I believe it already does this, which is the confusing part. It's a really big code, but here's a representative snippet:



$query = "SELECT aff, url, des, thumb FROM $table WHERE id = '$key'";
$result = mysql_query($query);
$exists = mysql_num_rows($result);
$dbrow = mysql_fetch_row($result);
mysql_free_result($result);

if ($exists) {

$dbrow = implode("|", $dbrow);
$postrow = "$aff|$url|$des|$thumb";

if ($dbrow == $postrow) {
$message[] = "";
} else if (trim($postrow, "|") == "") {
$query_2 = "DELETE FROM $table WHERE $table.id = $key LIMIT 1";
} else {
$url_id = array_search($url, $url_set);
if (($url_id == $key) || ($url_id === false)) {
$query_2 = "UPDATE $table SET url = '$url', des = '$des', thumb = '$thumb', aff='$aff' WHERE id = '$key'";
} else {
$message[] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <small>Error: duplicate url -- $url </small><br/>\n";
}
}

} else if (!$exists) {

if (trim($value, "|") != "") {
if (in_array($url, $url_set)) {
$message[] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <small>Error: duplicate url -- $url </small><br/>\n";
} else {
$query_2 = "INSERT INTO $table (id, star, aff, url, base_url, des, thumb) VALUES ('$key', '$passthru', '$aff', '$url', '$base_url', '$des', '$thumb')";
}
}
}
$result_2 = mysql_query($query_2);
if ($result_2) {
$message[] = "Successfully updated the database<br/>\n";
mysql_free_result($result_2);
} else {
$message[] = "Error updating $table: " . mysql_error() . "<br/>\n";
}
unset($aff); unset($url); unset($des); unset($thumb);



this is, as you can imagine, part of a foreach which loops about a hundred times.

I had connected to mysql by way of mysql_pconnect() until about four or five hours ago when i changed it to mysql_connect(), thinking that this would free up some memory. Should i change it back?

Twey
08-24-2007, 09:15 AM
Not the relevant part, I'm afraid.

Whether you use connect() or pconnect() here doesn't make a lot of difference.