0

My website was constantly running out of memory in random spots of code when the memory limit was set to 256M, so I changed it to 1024M ti see if it was an issue of space or some bad loop in the code... The website still ran out of memory after a while. What are some things I could do in order to not let the memory overflow?

I saw things about limiting requests but I think this does not solve the root of the problem. I will do that if it's my last option but I want to know what the best ways of troubleshooting this are.

PHP Version: 7.2.30

Apache Version: 2.4.41

Wordpress Version: 5.4.1

This is an image of the error shown on the website when the memory overflows: Error shown on website when the server runs out of memory

This is an example of the error (Keep in mind there are about 100 of these in the log file in one day and the location of the error varies (sometimes it's in a php file in the plugins folder, sometimes it's in the themes folder)):

[16-May-2020 19:16:22 UTC] PHP Fatal error:  Out of memory (allocated 21233664) (tried to allocate 4718592 bytes) in /var/www/html/wp-content/plugins/woocommerce/includes/log-handlers/class-wc-log-handler-file.php on line 21

EDIT: The logs also said that I did not have the XML service installed. I installed it but am not sure if that is the root of the problem.

Michel Chalfoun
  • 95
  • 1
  • 4
  • 11
  • Those are some seriously high memory munching levels. Your efforts would be best directed into debugging why so much memory is required to begin with. Limiting server requests wouldn't help, since the memory limit is per script, not per concurrent scripts in total. That said, it's really hard to point you anywhere in particular with no idea about what your website does. Please provide more info in your question. – Markus AO May 16 '20 at 19:49
  • Without any code it's just impossible to answer you. Try to review your code or try to find memory leak using some tools. One research on this site https://stackoverflow.com/questions/849549/diagnosing-memory-leaks-allowed-memory-size-of-bytes-exhausted This may help you ! – Erwan Daniel May 16 '20 at 19:51
  • @MarkusAO The thing is... This is a fairly new instance. All I have done has been copying the theme from another instance and using some plugins. The plugins are also things like Gravity Forms and WooCommerce... I'm not using any plugins that aren't fairly popular... I added some coded to the functions.php file but commenting it out did not change anything in terms of memory... I'm also running php 7.2 – Michel Chalfoun May 16 '20 at 20:25
  • @ErwanDaniel I can't mention 2 users in one comment so this is just to mention you in the above comment – Michel Chalfoun May 16 '20 at 20:26
  • To help others troubleshoot, can you please update your post with more info on (1) software used: PHP v.?, Apache v.?, Wordpress v.?, list of plugins, (2) samples error messages (file paths, really) where the error occurs. For the latter, you could also go over the PHP error log and post the full works into a paste somewhere. – Markus AO May 16 '20 at 21:36
  • Thanks for the input, I have added the info I can to the question. – Michel Chalfoun May 16 '20 at 22:26

2 Answers2

0

Wordpress should never consume that much memory on a single load. You're saying it's a fairly standard setup. Do you get the same levels of memory usage using a vanilla installation without themes and plugins? And then, do things spike after a given plugin? Build it up from a scratch and see if you can find the culprit (e.g. buggy plugin, plugin conflict, or faulty configuration).

If you want to dig in a bit deeper under the hood, short of using more involved PHP debugging tools like Xdebug, the built-in memory_get_usage() function is your friend. You can for example use a logging function like this (off the top of my head, briefly tested):

function log_mem($file, $line, $save = true) {
    static $iter = 0;
    $iter++;
    $usage = round(memory_get_usage() / 1048576, 2) . ' MB';
    $log = "[" . time() . "] {$iter}, {$file}#{$line}, {$usage}\n";
    $save && file_put_contents('tmp/php_memory.log', $log, FILE_APPEND);
    return $log;
}

log_mem(__FILE__, __LINE__); // to save into log file
echo log_mem(__FILE__, __LINE__, false); // to output only

Pop the log_mem() command into suspect locations. It will log the timestamp, the iteration number (ie. processing order), file name, line number and the current memory usage into a file. Like so:

[1589662734] 1, C:\server\home\more\dev\mem_log.php#14, 0.78 MB
[1589662734] 2, C:\server\home\more\dev\mem_log.php#18, 34.78 MB
[1589662734] 3, C:\server\home\more\dev\mem_log.php#22, 68.78 MB

You can then see where the spikes are triggered, and begin to fix your code.

If you don't care to add and remove the log commands (this obviously causes some processing overhead with repeated filesys access) over and over, you can make it conditionally run with a constant boolean switch, placed into any site-wide included file:

const MEM_LOG = true;
...
MEM_LOG && log_mem(__FILE__, __LINE__);

Remember to follow up and let us know what caused the memory leak. Good luck! ^_^

Markus AO
  • 4,771
  • 2
  • 18
  • 29
  • I have no experience with php and this is my first time creating an instance from scratch and hosting it on AWS. So, I am trying to disable plugins and see what is going to reduce the spike. If I can't find it that way I will definitely use your method with the function. However, when checking the memory usage, after setting the memory limit to 256, and using the free -m command, the total memory is at 983... which is much higher than the limit which does not make any sense... the top command shows me that only about 20% of the memory is being used... – Michel Chalfoun May 16 '20 at 21:16
  • Do you know why there is this big of a difference between the info I get from the free -m command compared to top? I need an accurate way of constantly knowing what the usage is for me to be able to troubleshoot this right way. Thanks a lot for the cooperation! – Michel Chalfoun May 16 '20 at 21:17
  • PHP memory limit is enforced `per script`. If you have 4 concurrent page loads, each using 256MB, your total usage will then be 1024MB for the duration. Aside that, be aware that there's a difference between a PHP script running out of memory, and your server running out of memory. For more insight on understanding results of `top` and `free -m`, please add samples into your original post. – Markus AO May 16 '20 at 21:33
  • I see. Based on the error name (PHP Fatal Error) and the error line I have added to the question, it seems to me that it's a php script running out of memory. I have added samples to the original post but would also like to not that the error shows up in different locations every time and is not related to one specific file of folder. – Michel Chalfoun May 16 '20 at 22:25
  • The above debugging steps apply, keep us posted when you make any discoveries. Now, looking at your error message. Are you quite sure your memory limits are actually being registered? (Run a phpinfo to double-check.) The error says, `"Out of memory (allocated 21233664) (tried to allocate 4718592 bytes)"`. In other words, allocated 21.23 MB, tried to allocate 4.71 MB... – Markus AO May 19 '20 at 12:58
0

People think Wordpress is easy. Even if you never touch the code, it is a very difficult system to manage and keep secure. Its complexity makes code customization very, very hard.

Your logs already shows you where the system is running out of memory.

This is an image of the error shown on the website when the memory overflows

What you said here illustrates that you are very inexperienced in operating PHP websites. Your installation should not be writing errors to the browser. This makes me think that you are way out of your depth in trying to resolve this.

You have posted this on a programming forum - implying you may be writing custom code. In which case the correct approach to resolving the error is to use a profiler to track where the memory is getting used up.

However if, in fact, there is no custom code on the site then you need to start by tracking memory usage (register a shutdown function for this) and start disabling themes and plugins until you find the problem.

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • I have enabled showing the errors on the website myself just to debug problems in the initial phase of the website. Is there a way you elaborate on the shutdown function? I think I understand what you mean by it but am not very sure. – Michel Chalfoun May 17 '20 at 04:55