简体   繁体   中英

My website keeps running out of memory (fatal error)

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: 服务器内存不足时网站上显示的错误

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.

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 (eg 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! ^_^

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM