You can add the following in index.php - make sure the log file path is correct
function shutdownCapture() {
global $lastQueryString;
$error = error_get_last();
if ($error !== null && strpos($error['message'], 'Allowed memory size') !== false) {
file_put_contents(MAGENTO_ROOT.'/var/log/oom.log', print_r(array($lastQueryString, $_REQUEST, $_SERVER, $error), true), FILE_APPEND);
mail('info@examplecom', 'Server Memory Error', print_r(array($lastQueryString, $_REQUEST, $_SERVER, $error), true), 'Cc: mycolleague@example.com');
}
}
register_shutdown_function('shutdownCapture');
and set these variables in the last reported function where the error occurs
+ global $lastQueryString;
+ $lastQueryString = $this->_stmt->queryString;
This excellent description comes from Sonassi
https://www.sonassi.com/blog/capturing-memory-exhaustion-in-magento
2 things you need to remember:
memory limit is set in basic php.ini file, most often in location like /etc/php/X.X/fpm/php.ini
and /etc/php/X.X/cli/php.ini
(this varies depending on the system and whether you use php as a service or not). Those settings also tells php whether you can override those default values with your custom ones in php.ini
file in your document root folder.
as you noticed above there are 2 paths for php.ini. If you use php-fpm then you have 2 independent settings for php run via apache/nginx as a response to http request and another set of settings for scripts run from the console (ie. bin/magento
or composer
).
Now if your system tells you memory limit is 128M then it does not lie. Either your default settings forbids you from settings memory limit in project folder or vhost settings points not to the file you are using to change the settings.
Best Answer
PHP's
memory_limit
setting should have no effect on Magento's performance. It only sets the high threshold of memory usage by PHP processes, and if Magento hogs more memory than that limit, you will get errors, but it changing it won't make things faster.I would strongly advice against modifying
memory_limit
to anything other than the recommended value unless you know what you're doing. Setting it too low may cause some of your Magento pages to crash, and setting it too high might cause your entire server to slow down to a halt in extreme cases.Instead, I would advise searching the Web on some tips on how to optimize Magento's performance. There are many relevant posts that can point you in the right direction. This post on my company's blog is one example, but there are many others. Enabling various forms of caching, using a CDN and optimizing your DB settings are probably the first places you want to look into.
BTW, I would also strongly advise against posting your server's phpinfo() output publicly for security reasons. You should remember to delete that file as soon as you can.