run PHP script from cli : php.ini memory_size not taken into account

run PHP script from cli : php.ini memory_size not taken into account

August 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
run PHP script from cli : php.ini memory_size not taken into account

PHP Memory Limit Issues: Debugging CLI Scripts

As experienced developers, we often find ourselves troubleshooting memory limit issues when running PHP scripts from the command line (CLI). This post will guide you through a common scenario where increasing the memory_limit isn’t working as expected.

The Challenge: Reindexing Magento with Memory Exhaustion Errors

You’re trying to run a Magento reindex script via CLI, but it’s throwing fatal errors due to memory exhaustion:

PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 72 bytes) in /home/karanta/www/karanta.fr/lib/Zend/Db/Adapter/Abstract.php on line 691

You’ve edited the memory_limit setting in your /etc/php5/cli/php.ini file and confirmed the new value in a phpinfo() script:

memory_limit => 2048M => 2048M

However, when you run the Magento reindex script again, it still throws the same memory error! Even after adding ini_set("memory_limit", -1); directly to your PHP script.

Troubleshooting Memory Limit Issues

Step 1: Verify PHP Configuration

Check if other PHP configurations are overriding your memory_limit setting. Look for any .htaccess or custom configuration files that might be affecting your CLI environment.

Step 2: Check System Resource Usage

Ensure you have enough free memory on the server:

free -k -h

And verify there’s no swap being used:

cat /proc/meminfo | grep SwapTotal

If resources are scarce, you may need to address this separately.

Step 3: Debug Memory Usage

Use tools like top or htop to monitor the memory usage of your PHP process while it’s running. This can help identify any unexpected memory leaks:

ps -aux | grep php

Advanced Techniques for Controlling Memory Limits

If standard methods aren’t working, you can use Zend_Memory to bypass default PHP memory limits:

require_once 'Zend/Memory.php';

$memoryManager = Zend_Memory::factory('none');
$memoryManager->setMemoryLimit(-1);

umask(0);

Remember to include this code at the beginning of your script. However, even with this approach, you might still face issues if the memory limit is being enforced by a higher-level system configuration.

Conclusion: When to Seek Further Help

If none of these steps resolve your issue, there could be other factors involved:

  1. PHP extensions or modules overriding default settings.
  2. System-level security policies restricting memory allocation.
  3. The Magento script itself having hardcoded memory limits.

In such cases, consider reaching out to the Magento community for specific advice tailored to your platform version and configuration. Websites like LaravelCompany.com often provide detailed guides on Magento performance optimization that you can refer to: Optimizing Memory Usage in Magento.

The key takeaway is: when dealing with memory limits, always verify the setting from multiple angles and consider both system-level and application-specific configurations.

Indexing Configuration Customers Errors

Our network