From: vijolicni dot oblak at gmx dot net
Operating system: Windows 2003
PHP version: 4.3.4
PHP Bug Type: Strings related
Bug description: [str_replace] Variable tracing 'optimization' causes 1.8GB memory
usage
Description:
------------
There is obviously some kind of tracing of what happens to variables
through their lifetime � a kind of optimization, I guess. But there is a
problem when doing str_replace on large html files (more than 600kb):
memory usage jumps from 350MB to 1.8GB according to Windows Task Manager
performance monitor. My computer has 512MB physical memory and having more
that one gigabyte of quickly changing data in swap file doesn�t help
performance at all. It renders my system to almost unresponsive.
I solved the problem by doing some �disoptimizations�: I put the contents
of variable $htmlfile, that str_replace was done on to a new variable
$tempvar, then I unset($htmlfile), and then reestablish $htmlfile as a new
variable with $tempvar as it contents and continue execution where ended
[�case� is used as a �goto� statement]. The set-unset code is commented.
The solution to the problem should be that Zend Engine or whatever module
is responsible for string tracing and/or memory management should check
how much !physical! memory is available and never waste more that
hundred(s) of available !physical! MBs on a script, but it should never
run into swap file memory gigabytes for �optimizations�.
[I have my swap file on Windows2003 set to 4GB if this is of any
relevance]
Reproduce code:
---------------
$htmlfile=str_replace("\t",' ',$htmlfile);
//if (sovertime(8)) savestate($destfilename,$htmlfile,10); return
outr(10); case 10:
while ($pos=strpos($htmlfile,' ')) {
$htmlfile=str_replace(' ',' ',$htmlfile);
if ($pos===false) break;}
//if (sovertime(8)) savestate($destfilename,$htmlfile,20); return
outr(20); case 20:
while ($pos=strpos($htmlfile,"\r\n\r\n")) {
$htmlfile=str_replace("\r\n\r\n","\r\n",$htmlfile);
if ($pos===false) break;}
//if (sovertime(8)) savestate($destfilename,$htmlfile,30); return
outr(30); case 30:
while ($pos=strpos($htmlfile,"\r\n ")) {
$htmlfile=str_replace("\r\n ","\r\n",$htmlfile);
if ($pos===false) break;}
//if (sovertime(8)) savestate($destfilename,$htmlfile,40); return
outr(40); case 40:
while ($pos=strpos($htmlfile," \r\n")) {
$htmlfile=str_replace(" \r\n","\r\n",$htmlfile);
if ($pos===false) break;}
$htmlfile=str_replace('<table ','<table width=100% ',$htmlfile);
$htmlfile=str_replace('<TABLE ','<TABLE width=100% ',$htmlfile);
$htmlfile=str_replace('<table>','<table width=100%>',$htmlfile);
$htmlfile=str_replace('<TABLE>','<TABLE width=100%>',$htmlfile);
//if (sovertime(8)) savestate($destfilename,$htmlfile,105);return
outr(105);case 105:
$htmlfile=str_replace('Š','�',$htmlfile);
$htmlfile=str_replace('š','�',$htmlfile);
$htmlfile=str_replace('Č','�',$htmlfile);
$htmlfile=str_replace('č','�',$htmlfile);
$htmlfile=str_replace('Ž','�',$htmlfile);
$htmlfile=str_replace('ž','�',$htmlfile);
//if (sovertime(8)) savestate($destfilename,$htmlfile,110);return
outr(110);case 110:
$htmlfile=str_replace('<table>','<table width=100%>',$htmlfile);
$htmlfile=str_replace('<TABLE>','<TABLE width=100%>',$htmlfile);
$htmlfile=str_replace('background:','bx:',$htmlfile);
$htmlfile=str_replace('mso-highlight:','bx:',$htmlfile);
$htmlfile=str_replace('text-align:justify','text-align:left',$htmlfile);
Expected result:
----------------
Usage of only of available physical RAM as a tempspace for optimizations.
Actual result:
--------------
Usage of only of more than 1GB of swapfile as a tempspace for
'optimization' of a script.
--
Edit bug report at http://bugs.php.net/?id=26577&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=26577&r=trysnapshot4
Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=26577&r=trysnapshot5
Fixed in CVS: http://bugs.php.net/fix.php?id=26577&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=26577&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=26577&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=26577&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=26577&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=26577&r=support
Expected behavior: http://bugs.php.net/fix.php?id=26577&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=26577&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=26577&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=26577&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=26577&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=26577&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=26577&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=26577&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=26577&r=float