Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process termination when using objects

Status
Not open for further replies.

peoplesrepublican

Programmer
Jan 29, 2005
6
GB
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
 
Do you have access to the server logs?
Check if there is a notification in the error log.
Inspect the output of phpinfo() and see what the the maximum memory size is.
Theoretically the use of objects should save memory.
 
I don't have access to the server logs, since I'm using a web host for my development. I have had a look at phpinfo() but I couldn't see any memory settings, which values should I be looking for?

The phpinfo() can be found here if you wish to look:


Thanks
 
memory_limit under php core.

Yours doesn't have an entry there however because your php wasn't compiled with --enable-memory-limit

How big are the objects you're talking about anyway? And how long is it taking, perhaps you're expiring your 30 second max execution time?

I would strongly advise setting up a local machine which have full control over which you can test on. That way you'll be able to narrow variables more readily.

Other than that, all i can suggest is posting the code if you're good with that... perhaps it's something in the code, or perhaps one of us can step through a debugger and see what happens, or at the very least see if it's replicatable or something particular to your server.

 
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 very much in advance for any help.

The local machine plan is good advice, no doubt. The reason I haven't done that so far is that I develop from different places (home, work etc) and so it's very convenient to have my dev area on a web server.
 
Sorry, I forgot to include the object size info. The class file for the Ad object ($ad) is 3425 bytes - I'm assuming that this relates directly to the size of the created object?

Cheers
 
I'm not a strong OO guy, especially in PHP... so hopefully someone more skilled will come by soon... but in the meantime here's what comes to mind.

Try putting an unset($this_ad); after you increment the array value.

Also, does PHP4 have a destructor function, or is that just in 5?

Another idea, would be to have a function like so...
Code:
function returnFilename($cat)
{
  $ad = get_ad($cat, false);
  return $ad->get_filename();
}

My thinking is simply, if it is a memory issue, leaving the function scope should cause the complete destruction of the object each time.

But I'm grasping... let me know if it helps, otherwise I'll keep my ears perked for someone who actually knows what they're talking about.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top