0

I've noticed a strange behaviour in XAMPP v3.3.2 with the PHP memory limit settings. My computer has Windows 10 installed and I have 8GB of memory. In my php.ini file the memory limit is set to 1024M. I also made this basic php file for testing:

<?php

echo ini_get('memory_limit');
exit;
?>

Running this file with different command line parameters, these are the outputs that I am getting:

php -d memory_limit=512M test.php outputs 512M
php -d memory_limit=2048M test.php outputs 2048M
php -d memory_limit=4096M test.php outputs the following error message:

PHP Fatal error:  Allowed memory size of 262144 bytes exhausted (tried to allocate 341351 bytes) in Unknown on line 0

Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 341351 bytes) in Unknown on line 0

php -d memory_limit=5000M test.php outputs 5000M but I still believe that it is not using 5000M of memory (runned some extended tests populating arrays with numbers, and both the test with 2048M set and with 5000M set exits at the same number, with the same message:

PHP Fatal error:  Out of memory (allocated 1457258496) (tried to allocate 134217728 bytes) in C:\xampp\htdocs\test\extended_test.php on line 733

Fatal error: Out of memory (allocated 1457258496) (tried to allocate 134217728 bytes) in C:\xampp\htdocs\test\extended_test.php on line 733


I've stumbled upon this when trying to allocate more memory to a script that should run on my computer. Any ideas what could be causing this?

Adam Baranyai
  • 3,635
  • 3
  • 29
  • 68
  • 1
    memory_limit of -1 disables the limit, use with caution. – Devon Bessemer Jan 09 '19 at 18:56
  • 2
    Generally, if you're mucking about with really high memory limits, there's something in your script to fix, like a loop that's not cleaning up after itself. – ceejayoz Jan 09 '19 at 18:56
  • @ceejayoz I am working on a script, strictly for personal use, in which I am implementing a minimax tree, and I am trying to push the depth limits as far as I can. – Adam Baranyai Jan 09 '19 at 18:59
  • @Devon I know about this, but I wouldn't want to use it, if not REALLY neccessary – Adam Baranyai Jan 09 '19 at 18:59
  • 1
    There's nothing inherently wrong with disabling the memory limit if you are programming correctly, especially in an isolated environment. If you don't want to disable the limit, it seems like the max supported (from attached dupe) is 4095M. – Devon Bessemer Jan 09 '19 at 19:03
  • @Devon if that is the case, then why is the program running with `memory_limit` set to `5000M`, and why is it exiting, indifferent of the limit set when `1457258496 bytes` meaning `1457.25M` was already allocated? Not trying to nitpick here, and I perfectly understand that there are limits, what I want to know, is this some kind of bug, or is there a reason why PHP is behaving like this. – Adam Baranyai Jan 09 '19 at 19:11
  • I saw your 4096M and thought it was a duplicate, didn't read into the 5000M setting. Out of memory is pretty self-explanatory, seems you actually didn't have 5000M free for PHP to use. On a Windows 10 PC with only 8GB of RAM, you'd rarely have 5000MB free for one program to allocate. – Devon Bessemer Jan 09 '19 at 21:55
  • @AdamBaranyai I have a similar problem on a xampp installation. The memory limit is set to 2048M, and the script stops with ~180M allocated, trying to allocate another 144M - the sum is far under 2048M. Why happens that? – Thomas Lauria Jan 16 '19 at 16:28
  • Finally I found out that somehow the apache config comes in touch. Changing the ThreadStackSize in apaches httpd-mpm.conf has an influence on which amount of used memory the PHP crashes, but I did not manage it to be increased finally to the configured 2048M in php.ini – Thomas Lauria Jan 16 '19 at 17:28

1 Answers1

1

Decreasing the ThreadsPerChild from 150 to a lower value solved it for me. Yet I could not explain the connections and background between all the settings in the MPM module, and finally "who" prevents the allocation of more memory.

<IfModule mpm_winnt_module>
    ThreadStackSize 8388608
    ThreadsPerChild 150  # ← try to decrease this value slightly on "Out of Memory" errors under windows.
    MaxConnectionsPerChild   0
</IfModule>
Thomas Lauria
  • 858
  • 1
  • 7
  • 15
  • This worked for me to get rid of the oom-error in the Symfony 5.4 profiler opening the logs. I found the mpm_winnt_module-section in [...]\apache\conf\extra\httpd-mpm.conf – manuxi Mar 07 '22 at 21:57