memory_limit
memory_limit is a configuration directive of the PHP language that sets the maximum amount of RAM a single script may consume during one request. When a script exceeds this threshold, PHP halts its execution and throws a fatal "Allowed memory size exhausted" error. This mechanism protects the server from a situation where one inefficient or malicious script would drain the entire machine's memory.
How memory_limit works
The value is expressed in bytes or with a unit suffix, for example 128M or 256M. The limit applies separately to each process handling a request — if the server processes dozens of requests in parallel, each has its own allowance, so the value must be chosen with the available RAM and the number of PHP-FPM processes in mind. The currently effective value can be checked in the output of the phpinfo() function. A setting of -1 means no limit, which is discouraged on production servers.
Practical application
The default 128 MB is enough for simple sites, but larger applications — Magento, stores with many plugins, importing large files or generating PDFs — may need 256 MB or more. The limit is changed in php.ini, in the PHP-FPM pool configuration, via a php_value directive in the .htaccess file (on servers with mod_php), or with ini_set() in code.
On shared hosting the upper bound of memory_limit is set by the provider so that one customer cannot dominate the resources of a shared machine. On a VPS or dedicated server the administrator has full control over this value. It is worth remembering that raising the limit treats the symptom rather than the cause — if a script uses excessive memory, a better solution is to optimize it, for example by processing data in smaller chunks.
Powiązane pojęcia
Najczęstsze pytania
How do you increase memory_limit in PHP?
You can change the value in php.ini (e.g. memory_limit = 256M), in the PHP-FPM pool configuration, via a php_value directive in .htaccess on servers with mod_php, or in code with ini_set(). On shared hosting the limit is often set in the control panel, and its upper bound may be capped by the provider for performance reasons.
What does the "Allowed memory size exhausted" error mean?
It means a PHP script tried to use more memory than memory_limit allows. Common causes are processing very large data sets, a memory leak, or inefficient code. The fix is to optimize the script (for example by processing data in chunks) or, when justified, to raise the limit.
