rectorphp/rector · error · InvalidConfigurationException

Memory limit "%s" cannot be set.

Error message

Memory limit "%s" cannot be set.

What it means

MemoryLimiter::adjust() applies the configured memory-limit option via ini_set('memory_limit', $value). ini_set() returns false when the current SAPI forbids changing that setting at runtime — most commonly Apache/mod_php or PHP-FPM pools where memory_limit is declared with php_admin_value (permanent level). A false return (after the format already validated) throws InvalidConfigurationException('Memory limit "%s" cannot be set.').

Source

Thrown at src/Util/MemoryLimiter.php:29

 */
final class MemoryLimiter
{
    /**
     * @see https://regex101.com/r/pmiGUM/1
     * @var string
     */
    private const VALID_MEMORY_LIMIT_REGEX = '#^-?\d+[kMG]?$#i';
    public function adjust(Configuration $configuration): void
    {
        $memoryLimit = $configuration->getMemoryLimit();
        if ($memoryLimit === null) {
            return;
        }
        $this->validateMemoryLimitFormat($memoryLimit);
        $memorySetResult = ini_set('memory_limit', $memoryLimit);
        if ($memorySetResult === \false) {
            $errorMessage = sprintf('Memory limit "%s" cannot be set.', $memoryLimit);
            throw new InvalidConfigurationException($errorMessage);
        }
    }
    private function validateMemoryLimitFormat(string $memoryLimit): void
    {
        $memoryLimitFormatMatch = Strings::match($memoryLimit, self::VALID_MEMORY_LIMIT_REGEX);
        if ($memoryLimitFormatMatch !== null) {
            return;
        }
        $errorMessage = sprintf('Invalid memory limit format "%s".', $memoryLimit);
        throw new InvalidConfigurationException($errorMessage);
    }
}

View on GitHub (pinned to 408fcb0ff1)

Solutions

  1. Remove --memory-limit / the memory limit option and let Rector use the SAPI's own limit
  2. Raise the limit where it is enforced: php_admin_value[memory_limit] in the FPM pool, or php.ini for mod_php, then restart the worker
  3. If you control the runtime, prefer plain memory_limit (changeable) over php_admin_value so ini_set can work
  4. As a workaround for tight limits, process fewer files per run (split paths) instead of raising memory
Defensive patterns

Strategy: try-catch

Try / catch

try {
    $memoryLimiter->adjust($configuration);
} catch (\Rector\Exception\Configuration\InvalidConfigurationException $e) {
    // SAPI forbids runtime changes: continue on the pool-configured limit instead of dying
    $logger->warning('memory_limit could not be raised at runtime; using server value: ' . ini_get('memory_limit'));
}

Prevention

When it happens

Trigger: Running rector with --memory-limit=2G (or the corresponding config/parameter) under an FPM pool that sets php_admin_value[memory_limit]; some CLI SAPIs with hardened restrictions likewise reject the ini_set.

Common situations: Deploying Rector as a web-triggered job (FPM worker); container images that hard-set memory_limit via admin directives; shared hosting where the limit is fixed.

Related errors


AI-assisted analysis of rectorphp/rector@408fcb0ff1 (2026-08-21). Data as JSON: /api/errors/e209c507f7133d2c. Report an issue: GitHub.