1

We use Apache (32-bit) on Windows in combination with PHP 7.2.9. It works most of the time but after a lot of refreshing (a lot is a random number of times each time apache gets restarted) we get this error: Fatal error: Out of memory (allocated 27262976) tried to allocate 4096 bytes in [random file, always a different one] on line x.

Weird thing is that it keeps giving the exact same error until we restart Apache, then it works for a couple of hours.

Also weird is that we set 512M as memory limit in the php.ini, but it says allocated 27262976 which is (exactly) 26MB. We have 2GB+ RAM free, so that isn't the problem.

It would be great if anyone knows how to solve this.

Thanks, Lars

Lars Jansen
  • 113
  • 1
  • 10
  • 1
    first step - run phpinfo in new file (phpinfo.php) in a new browser tab and see what php.ini is being loaded and what values are being set per option. Second step - you're not freeing up memory, I'm guessing a large script is in place and is rather resource-heavy, this is taking up all your memory. A lot of execution on these scripts = build up of memory which = leaks. – treyBake Oct 16 '18 at 09:31
  • Do you have any lingering or long-running processes that don't terminate correctly? – inquam Oct 16 '18 at 09:32
  • You have memory leak. Make sure you close your scripts and free up resources (e.g. opened DB connection/files). Also make sure your GarbageCollector is working as it should. – Justinas Oct 16 '18 at 09:32
  • Agree with all above. Use `unset()` for directing GC for better resource management. You can also try calling `memory_get_usage()` for checking memory usage. Usually it's just step-by-step debugging for finding leaks – Anton Oct 16 '18 at 09:35
  • Well, we use WordPress and this issue is in every single installation. The exact same code does not give this error on any other server. And besides that, why would it be out of memory after a couple of hours, after 100+ refreshes – Lars Jansen Oct 16 '18 at 09:36
  • @LarsJansen it can't be a WP issue - using the (disgusting) framework on a few sites - doesn't happen once - could also be environmental - maybe your server isn't configured properly to use the resources the scripts need - but then again - maybe your scripts shouldn't need to use a lot of resource? – treyBake Oct 16 '18 at 09:40
  • @LarsJansen to answer your edited commented bit - you have background processes that are triggered per user, when it ticks over the limit, the server falls over – treyBake Oct 16 '18 at 09:42
  • 2
    Do not forget that `memory_limit` refers to EACH and EVERY script, so if you have 5 simultaneous connections and 4 of them are allocating 400 MB each - then the fifth probably won't be able to allocate the same amount on your server (2GB RAM) – IVO GELOV Oct 16 '18 at 09:43
  • @ThisGuyHasTwoThumbs, indeed, it isn't a WordPress problem. It's weird that phpinfo just shows 512M as memory_limit. – Lars Jansen Oct 16 '18 at 09:45
  • @IVOGELOV, good point, but memory limit -1 gives the same issue after about the same time. Also changing to -1 with ini_set when the error occurs doesn't change a thing. – Lars Jansen Oct 16 '18 at 09:45
  • @LarsJansen have you reloaded/restarted apache/php-fpm after making .ini changes? Changes won't apply until then .. – treyBake Oct 16 '18 at 09:46
  • @ThisGuyHasTwoThumbs When I restart apache the problem is solved if I change php.ini or not. But I don't think I need to restart if I decide to use ini_set right? – Lars Jansen Oct 16 '18 at 09:48
  • @LarsJansen the problem will be solved because it clears everything and resets it to 0 - but apache still needs to be restarted if a modification to the .ini file is made. `ini_set` gets passed this - however, not every option can be set via ini_set – treyBake Oct 16 '18 at 09:49
  • Does using `mod_status` give a better insight ? https://serverfault.com/a/350566 and also https://stackoverflow.com/a/12178235 You can also try XDebug but I do not think it will help much in this situation – IVO GELOV Oct 16 '18 at 10:02
  • It says "Out of memory" rather than "Allowed memory size exhausted" which means it's having trouble allocating memory from the OS, rather than hitting the memory_limit setting, so INI changes won't help. We're getting the same error as you on 64bit PHP – EionRobb Oct 13 '19 at 21:41

1 Answers1

0

Most probably the memory just gets fragmented. (I had similar issues before.) You have to let garbage collection work more while your code is running.

One way to do that

You have to identify the part of the whole process where you create the biggest arrays or objects, and split it into multiple smaller steps. I don't know what your code does, but the important part is that PHP does garbage collection at certain steps, for example when a function returns and frees up its own environment. So if you, let's say, process 10000 files in a loop, it would be helpful to implement a queue system where you put in 100 files, call a function to deal with them, then go on processing the queue. Sounds silly, I know, but makes sense if you think about it.

Another way

You can allocate same-size structures for variable-length data, like 50-100k bricks that you only partially use; this way the memory won't get as fragmented. But garbage collection is a lot better and this would typically be his job.

Last resort

When your memory is about halfway exhausted - which you can check by calling memory_get_usage(true) - serialize the big structure you're using, unset the variable, then unserialize it back. This should sort out the allocation problems.

Hope some of the above helps.

dkellner
  • 8,726
  • 2
  • 49
  • 47