0

I am pulling down real estate properties from the Toronto Real Estate Board and I am using a LAMP stack with PHRETS on a 2GB Digital Ocean VPS. When I pull down the properties, I run some processes on them before saving them to a database. I have a cron job that runs the script every hour. Sometimes, the script runs flawlessly. Other times, I get this error, often within the first minute:

PHP Fatal error: Out of memory (allocated 131076096) (tried to allocate 12288 bytes) in /var/www/mysite.com/vendor/joshcam/mysqli-database-class/MysqliDb.php on line 1511

I've updated my php.ini file with memory_limit = 1500M

I am quite new to all of this but I am under the impression this should allow a script to allocate 1500MB, or 1.5GB of memory.

The error seems to be saying that the script tried to allocate 12288 bytes of a possible 131076096 bytes, but that doesn't add up, does it? How can I fix this? Do I need to upgrade my server or should 2GB of RAM be enough? Is there something I am missing?

Michael Lynch
  • 121
  • 1
  • 1
  • 3
  • 2
    Which php.ini did you update? Some distros have multiple copies. – Zoredache Jan 03 '17 at 17:26
  • You should consider optimizing your cron to not use as much RAM. Often, this can be done by putting something that's repeatedly executing (via a loop, for example) into a function so its variables get cleaned up at the end of each iteration. – ceejayoz Jan 03 '17 at 17:30

2 Answers2

2

I've updated my php.ini file with memory_allowed = 1500M

There's no such setting. The setting is named memory_limit. Be sure to restart your webserver (or PHP-FPM if you're using that) after making the change.

ceejayoz
  • 32,910
  • 7
  • 82
  • 106
  • Whoops. Yeah, that is what I had on the server, but typed it out wrong in my question. Thanks for mentioning that though. – Michael Lynch Jan 03 '17 at 17:39
  • 1
    @MichaelLynch Use `phpinfo()` on a test PHP file to make sure you've got the right `php.ini` (it will output which ones it's looking in) and be certain you restarted the webserver (and PHP-FPM if you're on that). – ceejayoz Jan 03 '17 at 17:40
  • Yep. All good. I've got the `phpinfo()` running and I can see `1500M`. I've also restarted the server. – Michael Lynch Jan 03 '17 at 17:45
  • @MichaelLynch Can you do `php -i` via the CLI too? Sometimes cron (as it runs on CLI) uses a different `php.ini`. – ceejayoz Jan 03 '17 at 17:57
  • I ran `php -i` and I see `memory_limit => -1 => -1` under Core. I presume the `-1` is in place of `1500M`, which is what I set. – Michael Lynch Jan 03 '17 at 19:41
1

Your PHP warning means that your script tried to allocate 12288 above the allowed maximum (default 128 MB).

You can raise the default maximum, but you need to be careful to not overallocated memory.

shodanshok
  • 47,711
  • 7
  • 111
  • 180
  • "It does not mean that all memory is allocated by this single script, rather than the PHP module itself is using all its allowed memory." This is *not* true. http://php.net/manual/en/ini.core.php#ini.memory-limit "This sets the maximum amount of memory in bytes that **a script** is allowed to allocate." – ceejayoz Jan 03 '17 at 17:28
  • 1
    @ceejayoz you are right, I incorrectly remembered that. I've edited my answer, thanks for pointing that out. – shodanshok Jan 03 '17 at 17:31
  • So my maximum is set to `1500M`...does that mean it requires `12288` bytes more than that? Each time the script runs, there is a different number of properties, so I'd expect that number to change each hour, but it doesn't. Also, what is `131076096` referring to? – Michael Lynch Jan 03 '17 at 17:43
  • `131076096` is 128 MB. Your script is allocating `131076096 + 12288` bytes, which is forbidden by your current PHP settings. – shodanshok Jan 03 '17 at 18:39