php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65723 Memory limit of string
Submitted: 2013-09-20 11:24 UTC Modified: 2013-09-25 08:03 UTC
From: m dot banaszek at smartmedia dot com dot pl Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: Irrelevant OS: Windows 8
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: m dot banaszek at smartmedia dot com dot pl
New email:
PHP Version: OS:

 

 [2013-09-20 11:24 UTC] m dot banaszek at smartmedia dot com dot pl
Description:
------------
The PHP manual now states, that strings are limited to 2GB. However in test 
script, I specify a memory limit to 800MB. I wrote a PHP application where memory 
usage is growing over time. Unfortunately the size of a single string variable 
doesn't  exceed memory limit of 300-400MB and my test stops. Testing my script on 
5.4.17,18,19 and  5.3.10 of PHP versions, but any of this versions don't 
supported  my memory limit or 2GB memory.

Test script:
---------------
<?php
ini_set('memory_limit', '800M');
$string = '1234567890';
$strb = '';
for ($i = 0; $i < 1000000; $i++)
    $strb .= $string;
$arr = array();
$i = 0;
$testType = 'string'; //'array'
while (1) {
    $i++;
    if ('string' == $testType) {
        error_log((strlen($string) / 1000000) . ' - ' .
                (memory_get_usage() / 1000000));
    } else {
        $arr[] = $strb . $i;
        error_log((strlen($string) / 1000000) . ' - ' .
                (memory_get_usage() / 1000000));
    }
    error_log((count($arr)) . ' - ' . (memory_get_usage() / 1000000)); }

Expected result:
----------------
Memory usage grow to about 800MB.

Actual result:
--------------
Memory error of 300-400MB. So the memory usage does not continually grow to 800MB.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-09-20 20:58 UTC] aharvey@php.net
-Status: Open +Status: Not a bug -Package: Systems problem +Package: Scripting Engine problem
 [2013-09-20 20:58 UTC] aharvey@php.net
There are some issues with your test: in its default "string" mode, memory usage 
never actually increases. If I change it to "array" mode, it behaves how the 
manual says it will — 81 iterations succeed ($strb is 10,000,000 bytes in size, 
and the effective memory limit is 838,860,800 bytes), and then the script errors 
out with an out of memory error (there are 810,000,000 bytes in the array, plus 
overhead for the array itself, plus $strb, plus general overhead). Bear in mind 
that the memory limit applies to the entire runtime, not individual variables.

A simpler, clearer test is this:

<?php
ini_set('memory_limit', '800M');
$buffer = '';

while (true) {
    $buffer .= str_repeat(' ', 10000000);
    printf("Length of buffer: %d\nMemory usage: %d\n", strlen($buffer), 
memory_get_usage());
}
?>

In short, this behaves as expected and documented.
 [2013-09-25 08:03 UTC] m dot banaszek at smartmedia dot com dot pl
Hello,
i'm very sorry there should be line : "$string .= $strb; ".
I tested your simplified code on my production machine. For 50 tests script fail 
always on 480MB
I use windows serwer 2008 r2 x64, iis 7, php 5.4.14.
When use array for test memory usage grows up to 1,9GB as expected.
I found temporary solution "http://www.ntcore.com/4gb_patch.php".
after patch, string test use 1,5 GB memory and array over 3GB
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu May 01 20:01:29 2025 UTC