php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28131 refcount limit is 64k
Submitted: 2004-04-23 23:46 UTC Modified: 2004-04-26 19:11 UTC
From: webmaster at macway dot com Assigned:
Status: Not a bug Package: Reproducible crash
PHP Version: 4.3.6 OS: Windows XP
Private report: No CVE-ID: None
 [2004-04-23 23:46 UTC] webmaster at macway dot com
Description:
------------
Despite proper setting of php.ini memory_limit, and 
despite having 2 GB of available ram, a script on 
windows server 2000 can't work with more than 64MB in 
memory.

There's no problems on a Mac OS X box.

That means you can't process huge volume of data in 
arrays for example if the php server is a window 2k box 
!

Reproduce code:
---------------
<?php



for ($i = 1; $i <= 1024; $i++) // I'm just making a 1024 Byte string
{
$chunk .='a';
}


for ($i = 1; $i <= 66000; $i++)  // I repeat it more than 65535 time and it fails
{
$toto .=$chunk;
}


echo $toto;

?>

Expected result:
----------------
A 65MB string should be returned.

This works perfectly on Mac OS X, but fails on Windows 
2000.

Actual result:
--------------
Windows 2000 aborts and return a "document contains no 
data".

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-24 00:29 UTC] fmk@php.net
I don't see any problems with PHP. Loading a 185 Mb file works fine and your test script is also doing fine.

What is the server environment you are using (Apache, IIS).
 [2004-04-24 02:18 UTC] webmaster at macway dot com
Apache 1.3.9

Do you use Windows ? (because it works without problems 
on linux/unix)
 [2004-04-24 12:24 UTC] derick@php.net
Upgrade apache from 1.3.9 to 1.3.29 first, then see if the problem persists.
 [2004-04-26 10:22 UTC] webmaster at macway dot com
I reproduced the bug using easyphp 1.7 on a windows xp 
machine. It crashes apache !
 [2004-04-26 12:02 UTC] wez@php.net
We don't support third party PHP distributions.
Please give the feedback we requested.
 [2004-04-26 16:55 UTC] webmaster at macway dot com
THE PROBLEM IS VERY SERIOUS : it crashes the php engine 
on Windows XP. It is reproductible.

I've redonne some tests and it appears that the web 
server is nit the issue (launching the php.exe without 
any webserver exhibits the problem).

I've you have 4 different variables, then the max size 
of each will be 64/4 = 16MB of ram max. That means the 
total ram consumed by the script has to be lower than 
64MB
 [2004-04-26 16:58 UTC] webmaster at macway dot com
Faster code exhibiting the problem :

<?php
for ($i = 1; $i <= 1024; $i++) // I'm just making a 1024 
Byte string
{
$chunk .='a';
}

for ($i = 1; $i <= 66000; $i++) // if you use 65000 it 
works
{
$tablo[]=$chunk;
}
echo "c'est fini";

?>
 [2004-04-26 17:32 UTC] wez@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

My 512MB laptop running XP will allocate 800MB (probably as much as the page file size, if I pushed it).

C:\php4>php -r "$x = str_repeat('*', 128 * 1024 * 1024); echo 'Done';"
Done
C:\php4>php -r "$x = str_repeat('*', 400 * 1024 * 1024); echo 'Done';"
Done
C:\php4>php -r "$x = str_repeat('*', 600 * 1024 * 1024); echo 'Done';"
Done
C:\php4>php -r "$x = str_repeat('*', 800 * 1024 * 1024); echo 'Done';"
Done
C:\php4>php -r "$x = str_repeat('*', 1280 * 1024 * 1024); echo 'Done';"
FATAL:  emalloc():  Unable to allocate 1342177281 bytes

It works fine; no crashes.  You must have something wrong with your configuration; check your memory_limit and error_reporting settings and talk to someone on a support channel to discover the reason that you have problems with this.
 [2004-04-26 17:53 UTC] webmaster at macway dot com
As I just said, I retested it with PHP 4.3.6 ALONE 
(without any webserver) using the php.exe under windows 
XP.

So it's clearly a problem with PHP 4.3.6 itself
 [2004-04-26 18:02 UTC] webmaster at macway dot com
> wez

Ok perharps its works with str_repeat, but it doesn't 
seem to work with array.

Please check with my code. Maybe I badly worded it,; 
maybe it's just with arrays, check with my lateest code 
please
 [2004-04-26 18:48 UTC] wez@php.net
ah, this bug.

It is to do with the COW reference created by the engine
when you copy $chunk around.  Since it is the same chunk, the engine saves copying the 1MB and adds a reference to the variable.  The problem is that ZE1 has a limit of 64k references before it overflows and causes this problem.

It has nothing to do with memory usage.
It is a known bug in this database (although I couldn't find it just now; it *is* in here somewhere).

This problem will not be fixed in PHP 4.x, but has already been addressed in PHP 5.

Workaround: use $toto .= str_repeat('a', 1024) inside the main loop, as this returns unique strings on each iteration.

 [2004-04-26 19:05 UTC] webmaster at macway dot com
>The problemis that ZE1 has a limit of 64k references
> before it overflows and causes this problem.

Thanks, but does your hypothesis explains this :

- It works perfectly under Mac OS X.3 (did not test any 
other unix). So it seems to be windows only (do you 
agree).
- if I have 4 different arrays, I can only have  4x16K 
records and not 4x64K records. So it seems the total 
number of record of all arrays is limited to 16K (and 
not 64K x number of different arrays).
I did test it with 4 different arrays containing 10000 
records and its ok, with 20000 records in each it 
crashes.

Thanks for your insight
 [2004-04-26 19:11 UTC] webmaster at macway dot com
Sorry, it does not work on Mac OS X either !
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Apr 25 23:01:29 2025 UTC