1

I have a strange memory limit issue in PHP. I have to read a lot of data into an array using a particular script, and I keep running out of memory.

My memory is now at 2048M in the php.ini file, and phpinfo() indicates it as such, yet, I keep getting this error:

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 142610432 bytes) in ... on line 173

Now - those two total about 680MB. This is far below the limit I set it to. Why would this error still occur?

Kobus Myburgh
  • 1,114
  • 1
  • 17
  • 46
  • 1
    Do you have some flag in your `.htaccess` for example `php_value memory_limit 512M` ? – angel.bonev Jan 16 '23 at 14:27
  • 1
    There is no .htaccess file. Both master and local value in phpinfo() shows 2048MB. My server has 32GB of RAM with otherwise < 5% utilization, so it is also not a memory shortage on the server. – Kobus Myburgh Jan 16 '23 at 14:35
  • 2
    Did you try setting it with `ini_set("memory_limit" , "2048M");` in most cases this will overwrite all other configuration. Is that what you mean with `in the config file` – angel.bonev Jan 16 '23 at 14:38
  • 1
    I just tried the init_set() you suggested, and it works, so then the question becomes "why?" Thank you, regardless, issue is solved for me, and no, the config file I meant php.ini file. – Kobus Myburgh Jan 16 '23 at 14:39
  • 2
    You can view how PHP Configuration is biuld in this answer https://stackoverflow.com/a/33933470/3768239 – angel.bonev Jan 16 '23 at 14:40
  • Thank you for the above link - very insightful - but if I run `phpinfo()` it means only the script (last option) would be different, not true? The phpinfo() indicated 2048M in both master and local value. – Kobus Myburgh Jan 16 '23 at 14:43
  • 1
    Just out of interest, compared to the `phpinfo()`, what does `ini_get('memory_limit')` return? – Patrick Janser Jan 16 '23 at 14:44
  • 1
    Very interesting, Patrick. ini_get('memory_limit') says 512MB... Quite strange. – Kobus Myburgh Jan 16 '23 at 14:45
  • 2
    Is this script run via CLI or via webserver? – brombeer Jan 16 '23 at 14:46
  • In my browser window - so not CLI. Very good question, as there are separate configs for CLI and non-CLI, right? – Kobus Myburgh Jan 16 '23 at 14:46
  • 3
    Yep, two different `php.ini`s for CLI and webserver. `phpinfo()` should tell you which one it uses – brombeer Jan 16 '23 at 14:51
  • Not using cli I think. `return php_sapi_name() === 'cli'` evaluates to false. – Kobus Myburgh Jan 16 '23 at 14:54
  • 2
    As it seems that `phpinfo()` isn't totally telling us the correct value, I would try and search recursively in all config files below `/etc` with a tool such as `rg` (ripgrep). You could do `cd /etc` and then simply `rg memory_limit`. You might have to do it under your document root also. – Patrick Janser Jan 16 '23 at 14:56
  • Might be a different (user) file being read. Maybe try https://www.php.net/manual/en/function.php-ini-scanned-files.php to see which ones got loaded. How do you serve? Apache? Nginx? PHP server? – brombeer Jan 16 '23 at 15:00
  • `print_r(php_ini_scanned_files())` is empty. Good suggestion. Thanks! Using Apache. – Kobus Myburgh Jan 16 '23 at 15:02
  • 1
    @angel.bonev - will you post as answer? Your comment got my functionality to work? – Kobus Myburgh Jan 16 '23 at 18:15

1 Answers1

1

Try using ini_set

Sets the value of the given configuration option. The configuration option will keep this new value during the script's execution, and will be restored at the script's ending.

ini_set("memory_limit" , "2048M"); 

In most cases ini_set rewrites all other PHP configurations

angel.bonev
  • 2,154
  • 3
  • 20
  • 30