|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 10:00:01 2025 UTC |
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.