PHP memory_get_peak_usage and ini_set ('memory_limit', '-1')

Recently I ran into memory allocation problems, so I started experimenting with the ini_set('memory_limit', value); directive ini_set('memory_limit', value); where I tried to enter values ​​incrementally. Now, browsing the Internet (and SO), I found out that I can put -1 as value . So, I did, and now the script runs completely to the end without breaking (before I used the memory allocation error).

I do not understand, however, that these two lines are at the end of the script file:

 $mem = memory_get_peak_usage(true); echo "Peak mem. usage: <b>" . round($mem / 1024 / 10124, 2) . "</b> MB"; 

produce around 10.8 MB , and when I look in /var/log/messages , I see this line:

 Nov 21 13:52:26 mail suhosin[1153]: ALERT-SIMULATION - script tried to increase memory_limit to 4294967295 bytes which is above the allowed value (attacker 'xx.xxx.xxx.xxx', file '/var/www/html/file.php', line 5) 

which means the script tried to allocate 4096 MB !

How can it be? And also, what interests me the most, why in this case the script stop stopped? This is due to ini_set('memory_limit', '-1'); ? I mean, I read that the placement is -1 , since value not recommended, and I know where the problem is in the script (reading too much data immediately in memory), and I will go and fix it with sequential reading, but I just puzzled by these differences in data, so I would appreciate it if someone could shed some light on him.

+4
source share
6 answers

This is because the suhosin patch uses its own "hard" maximum memory limit, suhosin.memory_limit .

From the configuration link:

Suhosin [...] prohibits setting memory_limit to a value greater than the script starting with when this option remains at 0.

In other words, if you change memory_limit so that it is larger than the upper limit of Sukhozin, it will simply assume that you are an “attacker” trying to do something suspicious.

+2
source

I think you used 10MB, but by putting -1 as value , you allowed your script to use the maximum available memory, say 4GB .

0
source

Using ini_set('memory_limit', '-1'); will not set a limit for scripts - of course, it will still be limited by hardware, but from the manual;

Please note that to limit the amount of memory set this directive to -1

What interests me the most, why in this case the script stop stopped?

The script did not stop running because it did not reach the maximum memory limit, since it is set to -1 .

0
source

memory_get_peak_usage - returns the peak of memory allocated by PHP.

memory_limit - increase the maximum memory allocation

 <?php if(ini_set('memory_limit', '-1')) { echo "memory_limit set to -1B"; } else { echo "ini_set() failed!" } ?> 
0
source

As described here: http://www.hardened-php.net/suhosin/configuration.html#suhosin.memory_limit

Suhosin will not allow you to set a value greater than the start of the script.

If you set suhosin.memory_limit to 4096 , then you can increase memory usage without receiving this warning

I assume that the script did not try to allocate 4096 MB , but its an incorrect warning about shuosin showing the maximum memory address limit of a 32-bit system . which runs php server which is 2 ^ 32

I hope this answers your doubts.

0
source
 $mem / 1024 / 10124 

it should be

 $mem / 1024 / 1024 

So we are talking about a peak of 100 MB.

Then 4,294,967,295 = 4 GB . I assume that everything you have (i.e. -1 ).

This means that these output messages have nothing to do with each other. If you want the warning to disappear, set the Suhosin memory limit above the PHP memory limit or disable Suhosin.

Sukhozin has a simulation mode that seems to be ON . If it is turned OFF , ALERT should stop the script.

0
source

Source: https://habr.com/ru/post/1447501/


All Articles