Process termination when using objects
Moderator: General Moderators
-
peoplesrepublican
- Forum Newbie
- Posts: 6
- Joined: Sat Jan 29, 2005 6:33 am
Process termination when using objects
Hi,
I have some code which runs through a loop. In each iteration of the loop, a function is called which returns a reference to an object (created from a class) to a variable.
The loop never runs to completion; after between 75-80 loops processing appears to just terminate...there are no error messages displayed. Am I hitting some kind of allowed memory or max number of objects limit?
Any help would be greatly appreciated.
Cheers
Tim
I have some code which runs through a loop. In each iteration of the loop, a function is called which returns a reference to an object (created from a class) to a variable.
The loop never runs to completion; after between 75-80 loops processing appears to just terminate...there are no error messages displayed. Am I hitting some kind of allowed memory or max number of objects limit?
Any help would be greatly appreciated.
Cheers
Tim
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
-
peoplesrepublican
- Forum Newbie
- Posts: 6
- Joined: Sat Jan 29, 2005 6:33 am
Here is a snippet of the code in question:
for ($i=0;$i<100;$i++)
{
$this_ad = get_ad($cat,false);
$sample_ads[$this_ad->get_filename()] += 1;
}
mysql_close();
$ad_keys = array_keys($sample_ads);
sort($ad_keys);
echo "<table border='1'>\n";
foreach ($ad_keys as $ad_key)
{
echo "<tr><td>$ad_key</td><td align='right'>".$sample_ads[$ad_key]."</td></tr>\n";
}
echo "</table>";
Basically, this call:
$this_ad = get_ad($cat,false);
returns an object into $this_ad. This is then used to keep a count of the filenames returned by this repeated call, and these counts are shown in the table generated at the foot of the script.
I should have put in my original post (sorry) that I don't think the max execution time is the problem, the script finishes running within about 10 secs and the server has max_execution_time set to 30 secs. I used set_time_out(0) and stuck some sleeps in the code, to eliminate the max_execution_time theory. The script ran for 94 secs and finished at around the same place (around 80 iterations out of an expected 100).
Thanks in advance for any help.
for ($i=0;$i<100;$i++)
{
$this_ad = get_ad($cat,false);
$sample_ads[$this_ad->get_filename()] += 1;
}
mysql_close();
$ad_keys = array_keys($sample_ads);
sort($ad_keys);
echo "<table border='1'>\n";
foreach ($ad_keys as $ad_key)
{
echo "<tr><td>$ad_key</td><td align='right'>".$sample_ads[$ad_key]."</td></tr>\n";
}
echo "</table>";
Basically, this call:
$this_ad = get_ad($cat,false);
returns an object into $this_ad. This is then used to keep a count of the filenames returned by this repeated call, and these counts are shown in the table generated at the foot of the script.
I should have put in my original post (sorry) that I don't think the max execution time is the problem, the script finishes running within about 10 secs and the server has max_execution_time set to 30 secs. I used set_time_out(0) and stuck some sleeps in the code, to eliminate the max_execution_time theory. The script ran for 94 secs and finished at around the same place (around 80 iterations out of an expected 100).
Thanks in advance for any help.
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
-
peoplesrepublican
- Forum Newbie
- Posts: 6
- Joined: Sat Jan 29, 2005 6:33 am
heh. I know it only goes up to 80 because I echoed out $i in the loop - but I took that out before posting the code. Even if I take out the entries to $sample_ad it still stops at around 80.
The most frustrating thing about this is that this isn't even a functional part of the system - it's just a benchmark script to prove the system works. I've wasted hours of valuable dev time on this seemingly insignificant script!
Any further help is greatly appreciated.
BTW another forum asked for the phpinfo() output - it's here if anyone's interested:
http://personal.crystalduck.com/phpinfo.php
The most frustrating thing about this is that this isn't even a functional part of the system - it's just a benchmark script to prove the system works. I've wasted hours of valuable dev time on this seemingly insignificant script!
Any further help is greatly appreciated.
BTW another forum asked for the phpinfo() output - it's here if anyone's interested:
http://personal.crystalduck.com/phpinfo.php
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
there is nothing that would make that loop stop short in your code, however the script could time out if it's taking too long.
make sure to have something like this in the code (for now)make sure this appears as close to the top of the php code as possible.
make sure to have something like this in the code (for now)
Code: Select all
error_reporting(E_ALL);
ini_set('display_errors','1');-
peoplesrepublican
- Forum Newbie
- Posts: 6
- Joined: Sat Jan 29, 2005 6:33 am
Thanks, I have done this. Unfortunately it didn't show any errors relating to why the script would stop, here's the end of the output:
<snip>
82
5.4493758678436
83
5.5015079975128
84
5.5538709163666
85
5.606085062027
I added back in the output of the loop var $i, and also a cumulative time - so you can see this stops a just over 5.6 seconds.
Thanks for all of your help so far, is there anything else I could try?
<snip>
82
5.4493758678436
83
5.5015079975128
84
5.5538709163666
85
5.606085062027
I added back in the output of the loop var $i, and also a cumulative time - so you can see this stops a just over 5.6 seconds.
Thanks for all of your help so far, is there anything else I could try?
-
peoplesrepublican
- Forum Newbie
- Posts: 6
- Joined: Sat Jan 29, 2005 6:33 am
This is the script I call in the browser:
get_ad:
get_ads_by_category:
Ad class:
Thanks a lot.
Code: Select all
<?php
include_once "su_lm_common.php";
include_once AD_CLASS;
$start = microtime_float();
error_reporting(E_ALL);
ini_set('display_errors','1');
// get params
$sample = $_REQUESTї"sample"];
// set default value
if ($sample == "")
{
$sample = 100;
}
$cat = $_REQUESTї"cat"];
// set default value
if ($cat == "")
{
$cat = 1;
}
$db = get_db_connection();
// show headers
echo "<b>New ad threshold: ".NEW_AD_THRESHOLD."</b><br><br>\n";
echo "<b>New ad share: ".NEW_AD_SHARE."</b><br><br>\n";
echo "<table border='1'>\n";
echo "<tr><th>Ad</th><th>Served</th><th>Clicked</th><th>Weighting</th></tr>\n";
// get ads for specified category
$ads = get_ads_by_category($cat);
foreach ($ads as $ad)
{
echo "<tr align='center'><td>".$ad->get_filename().
"</td><td>".$ad->get_serves()."</td><td>".$ad->get_clicks()."</td><td>".
$ad->get_weighting()."</td></tr>\n";
}
echo "</table><br><br>\n";
for ($i=0;$i<$sample;$i++)
{
echo $i."<br>\n".(microtime_float() - $start)."<br>\n";
$this_ad = get_ad($cat,false);
$sample_adsї$this_ad->get_filename()] += 1;
}
mysql_close();
$ad_keys = array_keys($sample_ads);
sort($ad_keys);
echo "<table border='1'>\n";
foreach ($ad_keys as $ad_key)
{
echo "<tr><td>$ad_key</td><td align='right'>".$sample_adsї$ad_key]."</td></tr>\n";
}
echo "</table>";
?>Code: Select all
function get_ad($cat,$external_use=true)
{
// this var is used to decide which set of ads to choose from,
// if any new (served <= NEW_AD_THRESHOLD) exist
$use_new_ads = false;
// get all ads for specified category
$ads = get_ads_by_category($cat);
// create empty new_ads array;
$new_ads = array();
// loop through all returned ads
foreach($ads as $ad)
{
// build two arrays of ads - those with serves greater than NEW_AD_THRESHOLD ($ads),
// and those with serves equal to or less than NEW_AD_THRESHOLD ($new_ads)
if ($ad->get_serves() > NEW_AD_THRESHOLD)
{
$adsї] = $ad;
}
else
{
$new_adsї] = $ad;
}
}
if (count($ads) > 0 && count($new_ads) > 0)
{
// decide which array of ads to use
if (rand(1,100) <= NEW_AD_SHARE)
{
$use_new_ads = true;
}
}
else if (count($ads) == 0 && count($new_ads) > 0)
{
$use_new_ads = true;
}
if ($use_new_ads)
{
$ad_to_use = rand(0,count($new_ads)-1);
$return_ad = $new_adsї$ad_to_use];
}
else
{
if (count($ads) > 0)
{
foreach ($ads as $ad)
{
for ($i=0;$i<$ad->get_weighting();$i++)
{
$ad_tombolaї] = $ad;
}
}
$ad_to_use = rand(0,count($ad_tombola)-1);
$return_ad = $ad_tombolaї$ad_to_use];
// if this is for external use, increment the serve count for the ad
if ($external_use)
{
$return_ad->increment_serves();
}
}
else
{
$return_ad = default_ad($cat);
}
}
return $return_ad;
}Code: Select all
function get_ads_by_category($cat)
{
// get all ads for the specified category, ordered by clicks/serves ratio desc
$sql = "SELECT id FROM ".AD_TABLE." WHERE category = $cat";
$result = mysql_query($sql);
while ($ad_id = mysql_fetch_row($result))
{
$ad = find_ad($ad_idї0]);
if ($ad !== false)
{
$adsї] = $ad;
}
}
return $ads;
}Code: Select all
<?php
class Ad
{
// member variables
var $id;
var $category;
var $filename;
var $width;
var $height;
var $submission_date;
var $serves;
var $clicks;
var $conversions;
var $cs_ratio;
var $weighting;
// member functions
// get and set
// id
function set_id($id)
{
$this->id = $id;
}
function get_id()
{
return $this->id;
}
// category
function set_category($category)
{
$this->category = $category;
}
function get_category()
{
return $this->category;
}
// filename
function set_filename($filename)
{
$this->filename = $filename;
}
function get_filename()
{
return $this->filename;
}
// width
function set_width($width)
{
$this->width= $width;
}
function get_width()
{
return $this->width;
}
// height
function set_height($height)
{
$this->height= $height;
}
function get_height()
{
return $this->height;
}
function set_submission_date($submission_date)
{
$this->submission_date = $submission_date;
}
function get_submission_date()
{
return $this->submission_date;
}
// serves
function set_serves($serves)
{
$this->serves = $serves;
}
function get_serves()
{
return $this->serves;
}
// clicks
function set_clicks($clicks)
{
$this->clicks = $clicks;
}
function get_clicks()
{
return $this->clicks;
}
// conversions
function set_conversions($conversions)
{
$this->conversions = $conversions;
}
function get_conversions()
{
return $this->conversions;
}
// cs_ratio
function set_cs_ratio($cs_ratio)
{
$this->cs_ratio = $cs_ratio;
}
function get_cs_ratio()
{
return $this->cs_ratio;
}
// weighting
function set_weighting($weighting)
{
$this->weighting = $weighting;
}
function get_weighting()
{
return $this->weighting;
}
// create_in_db - creates the ad in the db
// returns true if all goes well, false otherwise
function create_in_db()
{
echo $sql;
$sql = "INSERT INTO ".AD_TABLE." (".CATEGORY_FIELD.",".FILENAME_FIELD.
",".SUBMISSION_DATE_FIELD.")".
" VALUES ('".$this->category."','".$this->filename."',NOW())";
mysql_query($sql);
if (mysql_affected_rows() == 1)
{
// set the id of the ad
$this->id = mysql_insert_id();
return true;
}
else
{
return false;
}
}
// delete - deletes link
// returns true if all goes well, false otherwise
function delete()
{
$sql = "INSERT INTO ".DELETED_AD_TABLE." (".ID_FIELD.",".CATEGORY_FIELD.",".
FILENAME_FIELD.",".DELETION_DATE_FIELD.") ".
"SELECT ".ID_FIELD.",".CATEGORY_FIELD.",".FILENAME_FIELD.", NOW() FROM ".
AD_TABLE." WHERE id = ".$this->id;
mysql_query($sql);
if (mysql_affected_rows() == 1)
{
$sql = "DELETE FROM ".AD_TABLE." WHERE id = ".$this->id;
mysql_query($sql);
if (mysql_affected_rows() == 1)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
// increment_serves - increments serve_count by 1 in the db
// no return value (will never be checked)
function increment_serves()
{
$sql = "UPDATE ".AD_TABLE." SET ".SERVE_COUNT_FIELD." = (".SERVE_COUNT_FIELD.
" + 1) WHERE "."id = ".$this->id;
$result = mysql_query($sql);
}
}
?>