{"id":894,"date":"2012-11-06T23:47:52","date_gmt":"2012-11-07T05:47:52","guid":{"rendered":"http:\/\/blog.supportpro.com\/?p=894"},"modified":"2026-03-29T23:02:29","modified_gmt":"2026-03-30T05:02:29","slug":"increase-memory_limit-of-a-particular-script","status":"publish","type":"post","link":"https:\/\/www.supportpro.com\/blog\/increase-memory_limit-of-a-particular-script\/","title":{"rendered":"How to Increase PHP Memory Limit for a Specific Script ?"},"content":{"rendered":"\n<p>When working with large PHP applications, you may encounter the <strong>\u201cAllowed memory size exhausted\u201d<\/strong> error. This happens when a script consumes more memory than PHP allows. Increasing the PHP memory limit helps prevent script failures during heavy operations such as data processing, backups, or file uploads.<\/p>\n\n\n\n<p>This guide explains how to <strong>Increase PHP Memory Limit<\/strong> safely using different methods depending on your server setup.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is PHP <code>memory_limit<\/code>?<\/h2>\n\n\n\n<p>The <strong>memory_limit<\/strong> setting defines the maximum amount of memory a PHP script can use while running. Once this limit is reached, PHP stops execution to protect server resources and maintain stability.<\/p>\n\n\n\n<p>Most hosting environments set the default memory limit between <strong>64MB and 128MB<\/strong>, which may not be sufficient for demanding applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Causes of Memory Exhaustion<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Large database imports or exports<\/li>\n\n\n\n<li>Image processing or resizing<\/li>\n\n\n\n<li>Backup or migration scripts<\/li>\n\n\n\n<li>Bulk file uploads<\/li>\n\n\n\n<li>Complex plugins or frameworks<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Method 1: Increase PHP Memory Limit Using <code>ini_set()<\/code><\/h2>\n\n\n\n<p>The easiest way to increase PHP memory limit for a single script is by using the <code>ini_set()<\/code> function.<\/p>\n\n\n\n<p>Add this line immediately after the opening PHP tag:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>ini_set('memory_limit', '256M');<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How It Works<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Applies only to the current script<\/li>\n\n\n\n<li>Temporary change during execution<\/li>\n\n\n\n<li>Does not affect other PHP files<\/li>\n<\/ul>\n\n\n\n<p>This method is ideal for one-time tasks or specific scripts requiring extra memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 2: Increase PHP Memory Limit Using <code>php.ini<\/code><\/h2>\n\n\n\n<p>For a more permanent solution, modify or create a custom <strong>php.ini<\/strong> file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Steps<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a file named <code>php.ini<\/code> in your website\u2019s root directory.<\/li>\n\n\n\n<li>Add:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>memory_limit = 256M<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Applies to all scripts in the directory<\/li>\n\n\n\n<li>Suitable for shared hosting users<\/li>\n\n\n\n<li>Persistent configuration change<\/li>\n<\/ul>\n\n\n\n<p>Use this method when multiple scripts require increased memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 3: Increase PHP Memory Limit via <code>.htaccess<\/code><\/h2>\n\n\n\n<p>If your server runs <strong>Apache with mod_php<\/strong>, you can adjust the memory limit using the <code>.htaccess<\/code> file.<\/p>\n\n\n\n<p>Add the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php_value memory_limit 256M<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important Notes<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Works only on Apache with mod_php<\/li>\n\n\n\n<li>Not supported on PHP-FPM or CGI setups<\/li>\n\n\n\n<li>Incorrect configuration may cause a 500 error<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Which Method Should You Choose?<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><div class=\"pcrstb-wrap\"><table class=\"has-fixed-layout\"><thead><tr><th>Scenario<\/th><th>Recommended Method<\/th><\/tr><\/thead><tbody><tr><td>Single script needs more memory<\/td><td><code>ini_set()<\/code><\/td><\/tr><tr><td>Directory-wide configuration<\/td><td><code>php.ini<\/code><\/td><\/tr><tr><td>Apache mod_php hosting<\/td><td><code>.htaccess<\/code><\/td><\/tr><\/tbody><\/table><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Check Current PHP Memory Limit<\/h2>\n\n\n\n<p>You can verify the active memory limit using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>echo ini_get('memory_limit');<\/code><\/pre>\n\n\n\n<p>Open the file in your browser to view the current value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices Before Increasing Memory<\/h2>\n\n\n\n<p>Increasing limits should be done carefully. Excessive memory allocation can affect overall server performance.<\/p>\n\n\n\n<p><strong>Recommended practices:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Optimize inefficient PHP code<\/li>\n\n\n\n<li>Process data in smaller batches<\/li>\n\n\n\n<li>Remove unused plugins or modules<\/li>\n\n\n\n<li>Monitor server resource usage regularly<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Learning how to <strong>Increase PHP Memory Limit<\/strong> is essential for maintaining stable and efficient PHP applications. Whether you modify a single script with <code>ini_set()<\/code>, apply a directory-level change through <code>php.ini<\/code>, or configure <code>.htaccess<\/code>, always adjust limits responsibly.<\/p>\n\n\n\n<p>If scripts frequently exceed memory limits, consider optimizing the application to achieve better long-term performance instead of relying solely on higher memory allocation.<\/p>\n\n\n\n<p>If you require help, <a href=\"https:\/\/www.supportpro.com\/requestquote.php\">contact SupportPRO Server Admin<\/a><\/p>\n\n\n\n<p><span id=\"hs-cta-wrapper-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-wrapper\"><span id=\"hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-node hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\"><a href=\"https:\/\/cta-redirect.hubspot.com\/cta\/redirect\/2725694\/9d590242-d641-4383-94b4-8cfd62f0af6b\"><\/a><\/span><\/span><\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile is-vertically-aligned-center has-white-background-color has-background\"><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\">Partner with <strong>SupportPRO<\/strong> for 24\/7 proactive cloud support that keeps your business secure, scalable, and ahead of the curve.<\/p>\n\n\n\n<!--HubSpot Call-to-Action Code --><span class=\"hs-cta-wrapper\" id=\"hs-cta-wrapper-3350a795-db50-482f-9911-301930d1b1be\"><span class=\"hs-cta-node hs-cta-3350a795-db50-482f-9911-301930d1b1be\" id=\"hs-cta-3350a795-db50-482f-9911-301930d1b1be\"><!--[if lte IE 8]><div id=\"hs-cta-ie-element\"><\/div><![endif]--><a href=\"https:\/\/cta-redirect.hubspot.com\/cta\/redirect\/2725694\/3350a795-db50-482f-9911-301930d1b1be\" ><img decoding=\"async\" class=\"hs-cta-img\" id=\"hs-cta-img-3350a795-db50-482f-9911-301930d1b1be\" style=\"border-width:0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/2725694\/3350a795-db50-482f-9911-301930d1b1be.png\"  alt=\"Contact Us today!\"\/><\/a><\/span><script charset=\"utf-8\" src=\"https:\/\/js.hscta.net\/cta\/current.js\"><\/script><script type=\"text\/javascript\"> hbspt.cta.load(2725694, '3350a795-db50-482f-9911-301930d1b1be', {\"useNewLoader\":\"true\",\"region\":\"na1\"}); <\/script><\/span><!-- end HubSpot Call-to-Action Code -->\n<\/div><figure class=\"wp-block-media-text__media\"><img fetchpriority=\"high\" decoding=\"async\" width=\"904\" height=\"931\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup.png\" alt=\"guy server checkup\" class=\"wp-image-12943 size-full\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup.png 904w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-291x300.png 291w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-768x791.png 768w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-585x602.png 585w\" sizes=\"(max-width: 904px) 100vw, 904px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>When working with large PHP applications, you may encounter the \u201cAllowed memory size exhausted\u201d error. This happens when a script consumes more memory than PHP allows. Increasing the PHP memory&hellip;<\/p>\n","protected":false},"author":4,"featured_media":16792,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-894","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-miscellaneous"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/comments?post=894"}],"version-history":[{"count":12,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/894\/revisions"}],"predecessor-version":[{"id":16793,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/894\/revisions\/16793"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media\/16792"}],"wp:attachment":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media?parent=894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/categories?post=894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/tags?post=894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}