{"record":{"id":"e209c507f7133d2c","repo":"rectorphp/rector","slug":"memory-limit-s-cannot-be-set","errorCode":null,"errorMessage":"Memory limit \"%s\" cannot be set.","messagePattern":"Memory limit \"(.+?)\" cannot be set\\.","errorType":"validation","errorClass":"InvalidConfigurationException","httpStatus":null,"severity":"error","filePath":"src/Util/MemoryLimiter.php","lineNumber":29,"sourceCode":" */\nfinal class MemoryLimiter\n{\n    /**\n     * @see https://regex101.com/r/pmiGUM/1\n     * @var string\n     */\n    private const VALID_MEMORY_LIMIT_REGEX = '#^-?\\d+[kMG]?$#i';\n    public function adjust(Configuration $configuration): void\n    {\n        $memoryLimit = $configuration->getMemoryLimit();\n        if ($memoryLimit === null) {\n            return;\n        }\n        $this->validateMemoryLimitFormat($memoryLimit);\n        $memorySetResult = ini_set('memory_limit', $memoryLimit);\n        if ($memorySetResult === \\false) {\n            $errorMessage = sprintf('Memory limit \"%s\" cannot be set.', $memoryLimit);\n            throw new InvalidConfigurationException($errorMessage);\n        }\n    }\n    private function validateMemoryLimitFormat(string $memoryLimit): void\n    {\n        $memoryLimitFormatMatch = Strings::match($memoryLimit, self::VALID_MEMORY_LIMIT_REGEX);\n        if ($memoryLimitFormatMatch !== null) {\n            return;\n        }\n        $errorMessage = sprintf('Invalid memory limit format \"%s\".', $memoryLimit);\n        throw new InvalidConfigurationException($errorMessage);\n    }\n}\n","sourceCodeStart":11,"sourceCodeEnd":42,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/src/Util/MemoryLimiter.php#L11-L42","documentation":"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.').","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove --memory-limit / the memory limit option and let Rector use the SAPI's own limit","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","If you control the runtime, prefer plain memory_limit (changeable) over php_admin_value so ini_set can work","As a workaround for tight limits, process fewer files per run (split paths) instead of raising memory"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    $memoryLimiter->adjust($configuration);\n} catch (\\Rector\\Exception\\Configuration\\InvalidConfigurationException $e) {\n    // SAPI forbids runtime changes: continue on the pool-configured limit instead of dying\n    $logger->warning('memory_limit could not be raised at runtime; using server value: ' . ini_get('memory_limit'));\n}","preventionTips":["Skip --memory-limit when the FPM pool pins memory_limit via php_admin_value","Set generous memory_limit (not php_admin_value) in pools that run tooling","Wrap Rector invocations in wrappers that catch InvalidConfigurationException and retry without the flag"],"tags":["php","memory-limit","ini-settings","php-fpm","rector"],"backgroundTag":"memory-limit-override-failed","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}