Magento 1.9 – Fix PHP Fatal Error: Allowed Memory Size on Indexer.php

magento-1.9memory

Before reply please read scenario on Magento 1.8.X

command

php  indexer.php --reindex all
Product Attributes index was rebuilt successfully
Product Prices index was rebuilt successfully
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 128 bytes) in  /home/zeleyane/public_html/naturjoya.com/lib/Varien/Object.php on line 117

Information PHP

php -i | grep memory
memory_limit => 512M => 512M
Collecting memory statistics => No
suhosin.memory_limit => 0 => 0

Try put on index.php

ini_set('memory_limit', '512M');

I think there're any file of Magento with limit 512M.

Try looking for 256 or 268435456 on files, I don't see a correct file.

Best Answer

The problem comes from .htaccess. One of the values is php_value memory_limit 256M. This doesn't make sense on the first view, but on the second it is explainable.

I don't know why magento implemented this idiot thing, but what happend is, the following method reads the information from .htaccess and processes them:

\Mage_Shell_Abstract::_applyPhpVariables
protected function _applyPhpVariables()
{
    $htaccess = $this->_getRootPath() . '.htaccess';
    if (file_exists($htaccess)) {
        // parse htaccess file
        $data = file_get_contents($htaccess);
        $matches = array();
        preg_match_all('#^\s+?php_value\s+([a-z_]+)\s+(.+)$#siUm', $data, $matches, PREG_SET_ORDER);
        if ($matches) {
            foreach ($matches as $match) {
                @ini_set($match[1], str_replace("\r", '', $match[2]));
            }
        }
        preg_match_all('#^\s+?php_flag\s+([a-z_]+)\s+(.+)$#siUm', $data, $matches, PREG_SET_ORDER);
        if ($matches) {
            foreach ($matches as $match) {
                @ini_set($match[1], str_replace("\r", '', $match[2]));
            }
        }
    }
}