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?

16
  • 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 Commented Jan 16, 2023 at 14:38
  • 2
    You can view how PHP Configuration is biuld in this answer stackoverflow.com/a/33933470/3768239 Commented Jan 16, 2023 at 14:40
  • 2
    Is this script run via CLI or via webserver? Commented Jan 16, 2023 at 14:46
  • 3
    Yep, two different php.inis for CLI and webserver. phpinfo() should tell you which one it uses Commented Jan 16, 2023 at 14:51
  • 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. Commented Jan 16, 2023 at 14:56

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.