{"id":7243,"date":"2024-12-05T09:50:32","date_gmt":"2024-12-05T09:50:32","guid":{"rendered":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/haydenjames.io\/?p=7243"},"modified":"2025-02-04T15:47:20","modified_gmt":"2025-02-04T15:47:20","slug":"understanding-php-memory_limit","status":"publish","type":"post","link":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/understanding-php-memory_limit\/","title":{"rendered":"PHP memory_limit \u2013 understanding and increasing"},"content":{"rendered":"<p>PHP&#8217;s memory_limit is <em>per-script<\/em>, just as a highway&#8217;s speed limit is <em>per-vehicle<\/em>. For example, although PHP\u2019s memory limit may be set to 1GB, that does not mean that scripts will pile up to use that 1 GB. Let\u2019s take a closer look at understanding PHP\u2019s memory_limit setting.<\/p><div class=\"hayden-after-paragraph hayden-entity-placement\" id=\"hayden-835047183\"><div data-nosnippet id=\"hayden-101293639\" style=\"margin-left: auto;margin-right: auto;text-align: center;\"><a data-no-instant=\"1\" href=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/betterstack.com\/\" rel=\"noopener nofollow sponsored\" class=\"a2t-link\" target=\"_blank\" aria-label=\"Linux Logs and metrics: Better Stack\"><img fetchpriority=\"high\" loading=\"eager\" decoding=\"async\" src=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-content\/uploads\/2025\/04\/logs-and-metrics-wide.png\" alt=\"Linux Logs and metrics: Better Stack\"  srcset=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-content\/uploads\/2025\/04\/logs-and-metrics-wide.png 868w, https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-content\/uploads\/2025\/04\/logs-and-metrics-wide-300x104.png 300w, https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-content\/uploads\/2025\/04\/logs-and-metrics-wide-560x194.png 560w\" sizes=\"(max-width: 868px) 100vw, 868px\" width=\"868\" height=\"300\"  style=\"display: inline-block;\" \/><\/a><\/div><div align=\"center\" style=\"font-style: italic; font-size: 0.85em; font-weight: normal; margin-top: 4px;\">\r\n\tAdvertisement\r\n<\/div><\/div>\n<h2><strong>PHP memory_limit is a per-script setting<\/strong><\/h2>\n<p>PHP.net&#8217;s documentation puts it this way:<\/p>\n<pre>This sets the maximum amount of memory in bytes that <strong>a script<\/strong> is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server...<\/pre>\n<p class=\"para\">Source: <a href=\"https:\/\/round-lake.dustinice.workers.dev:443\/http\/php.net\/manual\/en\/ini.core.php#ini.memory-limit\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/round-lake.dustinice.workers.dev:443\/http\/php.net\/manual\/en\/ini.core.php#ini.memory-limit<\/a><\/p>\n<p>Unlike, say, MySQL&#8217;s innodb_buffer_pool settings, the PHP memory_limit setting is <strong>not<\/strong> a storage space where multiple PHP scripts pool from or grow within. Rather, it&#8217;s a\u00a0<strong>per-script limit<\/strong>. PHP memory_limit is the maximum amount of server memory a\u00a0<strong>single<\/strong>\u00a0PHP script is allowed to consume. When blocked, the resulting error output looks something like this:<\/p>\n<pre>Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in \/path\/to\/php\/script<\/pre>\n<p>Or like this:<\/p>\n<pre>PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in \/path\/to\/php\/script<\/pre>\n<p>So, for example, if two or more scripts are requested simultaneously, each is completely independent of the other. They do not share the memory_limit setting. Remember, <a href=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/php-performance-additional-cpu-cores-vs-faster-cpu-cores\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">PHP is not designed for and does not support multithreading<\/a>. Thus, if five PHP scripts simultaneously use 100 MB <em>each<\/em>, that would consume 500 MB of total PHP memory usage on the server. Yet, in this scenario, a PHP memory_limit setting of &#8216;256M&#8217; per script would not be hit.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-11056 size-large\" title=\"PHP memory_limit\" src=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-content\/uploads\/2017\/09\/php_memory_limit-1-868x390.jpg\" alt=\"PHP memory_limit\" width=\"868\" height=\"390\" srcset=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-content\/uploads\/2017\/09\/php_memory_limit-1.jpg 868w, https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-content\/uploads\/2017\/09\/php_memory_limit-1-300x135.jpg 300w\" sizes=\"auto, (max-width: 868px) 100vw, 868px\" \/><br \/>\n<em>PHP memory_limit is per-script, just as a highway&#8217;s speed limit is per-vehicle.<\/em><\/p>\n<p>That said, for scripts that request other PHP scripts inline using require, include, or include_once, this limit is then inherited and shared by all included scripts that are dependent on the parent script.<\/p>\n<p><span style=\"color: #000080;\"><em>&#8220;The include statement takes all the text\/code\/markup that exists in the specified file and copies it into the file that uses the include statement. The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.&#8221; &#8211; <a href=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/www.w3schools.com\/php\/php_includes.asp\" target=\"_blank\" rel=\"noopener noreferrer\">W3schools<\/a>.<\/em><\/span><\/p>\n<p><span style=\"color: #000080;\"><em>&#8220;The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.&#8221; &#8211; <a href=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/www.php.net\/manual\/en\/function.require-once.php\" target=\"_blank\" rel=\"noopener noreferrer\">php.net<\/a><\/em><\/span><\/p>\n<p>Now, regarding the original example mentioned at the outset. A lower setting of 256M is always better because if PHP scripts are trying to use more than 256M, those scripts would now return memory limit exceeded errors. In the above issue, that was not the case, so regardless of the 256M or 1G memory_limit setting, it only comes into play if there are inefficient scripts.<\/p>\n<p>Fortunately, the PHP memory_limit setting will block inefficient code, alerting you to optimize your code. Until fixed, you may want to temporarily increase PHP memory_limit to avoid your web application becoming unusable due to PHP out-of-memory errors.<\/p>\n<p>If you don&#8217;t have available memory on your server, you&#8217;ll sometimes be faced with deciding whether to increase PHP memory_limit to meet the requirements of scripts or to optimize your code. It would be best if you always optimized as the preferred option when possible. Also, you can increase PHP&#8217;s memory limit for specific websites. One method would be to place a php.ini file in the site&#8217;s webroot. You can even set the limit for specific scriptname.php. For example, using <a href=\"https:\/\/round-lake.dustinice.workers.dev:443\/http\/php.net\/manual\/en\/function.ini-set.php\" target=\"_blank\" rel=\"noopener noreferrer\">ini_set(&#8216;memory_limit&#8217;,&#8217;256MB&#8217;)<\/a>.<\/p>\n<h2>How to increase PHP\u00a0<strong>memory_limit<\/strong><\/h2>\n<p>To increase the PHP memory limit setting, edit your PHP.ini file. Increase the default value (example: Maximum amount of memory a script may consume = 256 MB) of the PHP <strong>memory limit<\/strong> line in php.ini.<\/p>\n<pre>memory_limit = 2048M<\/pre>\n<p>Alternatively, you can edit your .htaccess file (Not recommended. See: <a title=\"Apache Performance: Disable .htaccess\" href=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/disable-htaccess-apache-performance\/\" rel=\"bookmark\">Apache Performance: Disable .htaccess<\/a>)<\/p>\n<pre>php_value memory_limit 2048M<\/pre>\n<p>If you don&#8217;t have access to these files or the experience to make this change, you can contact your web host and ask them to increase your PHP memory limit.<\/p>\n<h2>Conclusion<\/h2>\n<p>While your PHP configuration might boast a generous memory limit, it&#8217;s important to grasp that individual scripts don&#8217;t accumulate memory like a shared pool. Instead, each script operates within its allocated memory boundary.<\/p>\n<p>This per-script nature ensures that one script&#8217;s inefficiency won&#8217;t hinder others. If multiple scripts are executed simultaneously, they remain independent and won&#8217;t collectively breach the memory limit. However, when scripts call upon other scripts using &#8216;require&#8217; or &#8216;include,&#8217; the parent script&#8217;s memory limit extends to its dependencies.<\/p>\n<p>The PHP memory_limit setting serves as a guardian against runaway scripts, throwing &#8216;memory limit exceeded&#8217; errors when needed. It&#8217;s a valuable tool for identifying and rectifying inefficient code. When facing such errors, it&#8217;s wise to temporarily increase the memory limit, allowing your web application to remain functional while you optimize your code.<\/p><div class=\"hayden-end-of-article hayden-entity-placement\" id=\"hayden-4229574319\"><div data-nosnippet id=\"hayden-418781300\" style=\"margin-left: auto;margin-right: auto;text-align: center;\"><a href=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/www.manageengine.com\/it-operations-management\/infrastructure-observability\/server-observability.html?utm_source=linuxblog&#038;utm_medium=bannerad&#038;utm_campaign=ServerObservability\" data-bid=\"1\" data-no-instant=\"1\" rel=\"noopener nofollow sponsored\">\r\n  <img fetchpriority=\"high\" src=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/static.linuxblog.io\/wp-content\/uploads\/2026\/06\/868\u00d7300V3_x1.png\"\r\n       srcset=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/static.linuxblog.io\/wp-content\/uploads\/2026\/06\/868\u00d7300V3_x1.png 1x,\r\n               https:\/\/round-lake.dustinice.workers.dev:443\/https\/static.linuxblog.io\/wp-content\/uploads\/2026\/06\/868\u00d7300V3.png 2x\"\r\n       alt=\"ManageEngine Site24x7 - Fix Linux issues in minutes, not hours!\"\r\n       width=\"868\" height=\"300\"\r\n       loading=\"eager\"\r\n       style=\"display:block;\">\r\n<\/a><\/div><div align=\"center\" style=\"font-style: italic; font-size: 0.85em; font-weight: normal; margin-top: 4px;\">\r\n\tAdvertisement\r\n<\/div><\/div>\n<p>In conclusion, PHP memory_limit is a crucial element in <a href=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/monitoring-php-performance-diagnosing-bottlenecks\/\" target=\"_blank\" rel=\"noopener\">maintaining the performance<\/a> and <a href=\"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/php-8-compatibility-check-and-performance-tips\/\" target=\"_blank\" rel=\"noopener\">reliability<\/a> of your web applications. By comprehending its per-script nature and using it judiciously, you can ensure that your websites run smoothly and efficiently, providing a seamless experience for users and developers alike.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: right;\"><em>Originally published: September 18th, 2017 | <\/em><em>Last updated: December 5th, 2024.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP&#8217;s memory_limit is per-script, just as a highway&#8217;s speed limit is per-vehicle. For example, although PHP\u2019s memory limit may be set to 1GB, that does not mean that scripts will pile up to use that 1 GB. Let\u2019s take a closer look at understanding PHP\u2019s memory_limit setting. PHP memory_limit is a per-script setting PHP.net&#8217;s documentation [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"publish_to_discourse":"0","publish_post_category":"54","wpdc_auto_publish_overridden":"1","wpdc_topic_tags":"","wpdc_pin_topic":"","wpdc_pin_until":"","discourse_post_id":"4256","discourse_permalink":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxcommunity.io\/t\/php-memory-limit-understanding-and-increasing\/2034","wpdc_publishing_response":"","wpdc_publishing_error":"","footnotes":""},"categories":[1,11],"tags":[54,45,92],"class_list":["post-7243","post","type-post","status-publish","format-standard","hentry","category-articles","category-linux","tag-memory","tag-performance","tag-php"],"_links":{"self":[{"href":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-json\/wp\/v2\/posts\/7243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-json\/wp\/v2\/comments?post=7243"}],"version-history":[{"count":4,"href":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-json\/wp\/v2\/posts\/7243\/revisions"}],"predecessor-version":[{"id":19750,"href":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-json\/wp\/v2\/posts\/7243\/revisions\/19750"}],"wp:attachment":[{"href":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-json\/wp\/v2\/media?parent=7243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-json\/wp\/v2\/categories?post=7243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/linuxblog.io\/wp-json\/wp\/v2\/tags?post=7243"}],"curies":[{"name":"wp","href":"https:\/\/round-lake.dustinice.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}