Edit report at https://bugs.php.net/bug.php?id=77879&edit=1

 ID:                 77879
 Updated by:         ni...@php.net
 Reported by:        xxalfa at gmail dot com
 Summary:            Usable memory
 Status:             Not a bug
 Type:               Bug
 Package:            Testing related
 Operating System:   Windows
 PHP Version:        7.3.4
 Block user comment: N
 Private report:     N

 New Comment:

@xxalfa With your script, this is the last output I get:

memory_allocated -- 10 MByte -- memory_used -- 6740856 -- data_length -- 6290432

Fatal error: Allowed memory size of 16777216 bytes exhausted at 
/home/nikic/php-7.4/Zend/zend_string.h:205 (tried to allocate 6291488 bytes) in 
/home/nikic/php-7.4/t022.php on line 65

As you can see, the attempt is to allocate 6MB additional memory, which ends up 
going above the memory limit. Even though the string doesn't take up the full 
16MB, in order to reallocate it (if it's not possible to do so in-place) we 
first have to create a new larger allocation, copy the string and then release 
the old one. So you may need about 2x more memory to handle the reallocation.


Previous Comments:
------------------------------------------------------------------------
[2019-04-14 03:20:53] xxalfa at gmail dot com

Reply to requi...@php.net
I am glad of every answer, only we both do not speak the same language. PHP can 
allocate as much memory as it needs. The amount of data I want to process 
requires knowing the amount of memory consumed to initiate paging operations. 
Any write access to the hard drive takes time, so I want to use as much memory 
as possible.

Reply to ni...@php.net
I thank you for your answer, which I now understand the way PHP works. I hope 
you can briefly answer my second question? When I open the task manager, I can 
keep track of how much memory a script consumes. Although PHP reserves full 
RAM, I can only fill it up to 40% at 16 MB. If I set the limit to 41% and fill 
the memory, I get a fatal error. In the Task Manager, a value of about 12 MB is 
given, why does not "memory_get_usage" provide the true consumption of 12 MB?

------------------------------------------------------------------------
[2019-04-12 07:48:07] ni...@php.net

Memory allocations are restricted to certain sizes for performance reasons. 
Small allocations are rounded up the next largest bin size. When the memory is 
reallocated with a size that still fits within the same bin, the reallocation 
is performed in-place and the reported memory usage will not change.

------------------------------------------------------------------------
[2019-04-12 03:06:55] requi...@php.net

No, the memory does not increase by 1024 each time. It increases by whatever 
amount PHP allocates in order to expand the string by another 1024 bytes. As 
your output shows, the last step it went up by 4096 - enough for approximately 
4 increases.

Should it have only allocated 1k? Is it bad that PHP is reserving more memory 
than the precise amount it needs at that given moment?

------------------------------------------------------------------------
[2019-04-12 02:58:35] xxalfa at gmail dot com

The script executes successfully because it has detected because the level of 
memory has not changed. However, the level increases by 1024 per cycle, if the 
function were to work properly, the script would continue to work. That's why 
it's a bug.

------------------------------------------------------------------------
[2019-04-12 02:26:32] requi...@php.net

Your script is quitting because PHP didn't need to allocate more memory to 
handle another 1k characters. Why is that a bug?

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=77879


--
Edit this bug report at https://bugs.php.net/bug.php?id=77879&edit=1

Reply via email to