DannyCarlton.net Feedback Forums | register | login
phpBible.org

    Return to Page 4
Possible solution in response from hosting provider
Jul. 7th, 2004   1:04pm

If ever someone wants a good, helpful ISP, go to TigerTech! Click on the link at mistfa.org to help me out...

This is what they told me:

Thank you for the note! Sorry to hear you’re having trouble.

We took a look at the script and ran some tests (thanks for the complete problem description -- that was very useful), and what happened was that the script was running out of memory. It was reaching the 30 MB memory limit allowed for a CGI/PHP script and being "killed" by the Web server; unfortunately, PHP doesn’t display an appropriate error message when this happens (Perl scripts do, for example).

Anyway, like us, you’re probably surprised that the script uses over 30 MB of RAM, considering that it’s only reading a 9 MB file. It seems that the str_replace() functions of the form

$av_text[$i]=str_replace ...

... cause it to use vast amounts of RAM -- each time you call

str_replace() like this, it uses about three times the memory the original string required. And since the script doesn’t clean up the extra memory used by $av_text[$i] after each line, it eventually runs out of memory.

Using a temporary variable that’s reset each time through the loop would solve this problem. For example, something like this:

for($i=0;$i<=31117;$i++)

{

echo $i." ";

$temp= $av_text[$i];

$temp=str_replace("\n", ’’, $av_text[$i]);

$temp=str_replace("\r", ’’, $av_text[$i]);

$temp=str_replace(’INTO bible VALUES’, "INTO $table_name VALUES", $av_text[$i]);

if($temp){$result = mysql_query($temp) or die("Query failed ($i): " . mysql_error());} }

I actually tested (something very close to) this and it works fine; it is not measurably slower, and it uses less than 10 MB of RAM instead of close to 35 MB.

So I’d say that in general, you want to avoid directly modifying text in a multi-MB array, as it uses far too much RAM. Copy it into a temporary variable and modify that instead, and it works much better.

Hope this is useful. (I should also mention that officially, we don’t give PHP code advice, for obvious reasons -- but if we’re not too busy, we usually make exceptions when someone has an "interesting" problem like this one... I certainly learned something about PHP.)

Mike Ewer (68.251.170.237)


    Return to Page 4
This Thread

Post a Message
Name:

Subject:

Message:


page processed in 0.00193214416504 seconds
Return to DannyCarlton.net main page