O'Reilly

Increase PHP Memory Allowance Using ini_set()

By on  

On rare occasion, I need to up the memory allowed for PHP. Usually it's when I deal with large files and don't necessarily care about optimizing the file reading process but simply getting the file opened. There's an easy way to increase to amount of memory allowed to PHP right in your script:

ini_set('memory_limit','16M');

The above code will increase the maximum amount of memory available to PHP to 16 MB. Again, the setting is only adjusted for the running script.

Track.js Error Reporting

Recent Features

  • Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() – this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

  • Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

Incredible Demos

  • Image Reflection with jQuery and MooTools

    One subtle detail that can make a big difference on any web design is the use of image reflections. Using them too often can become obnoxious but using reflections on large, "masthead" images is a classy enhancement. Unfortunately creating image reflections within your...

  • spellcheck Attribute

    Many useful attributes have been provided to web developers recently:  download, placeholder, autofocus, and more.  One helpful older attribute is the spellcheck attribute which allows developers to  control an elements ability to be spell checked or subject to grammar checks.  Simple enough, right?...

Discussion

  1. chuck

    I had to do this recently for a client who had joomla (yuck) installed. I just posted this in the top of their main index.php file so it was loaded on every page. It’s really good as a quickfix if you’re using something like Gallery or Joomla along with php-cgi, since php-cgi doesnt let you use htaccess.

  2. Thank you o-b-wan, both for this post, and the kindly remonstration given on the other post that sent me here.

    :)

    Michael

  3. This worked great David.

    I had to place it below some other “session_start();” code in the index2.php file of a troublesome joomla installation, but it worked just as you offered.

    memory_limit Local 16M; Master 8M

    Thanks again o-b-wan.

    Off to look for the next pebble…
    :)

    Michael

  4. Sam

    I have a problem with wordpress installing plugins where I get the error:

    Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 122880 bytes) in /var/virtual/web/w1032/html/wp-content/plugins/wp-postratings/wp-postratings.php on line 872
    

    Which file should I place the code in? And I obviously need to increase it past 16MB so can I just put in:

    ini_set('memory_limit','32M');  
    

    maybe wp-config.php or the actual plugin? wp-postrating.php?

  5. Sam

    I cannot install plugins on wordpress due to a memory error.

    I can’t get to php.ini, which file do you know should I put

    ini_set(‘memory_limit’,’24M’)

    in?

    wp-config, the plugin script or all the wp php files?

    thanks!

  6. Akshay

    @Sam: You have to put

    ini_set(’memory_limit’,'24M’);
    

    this line of code in the file/page, which on calling gives the error.

  7. Tob-e

    I’ve got a strange behaviour on PHP 5.2.11.
    The php.ini set the memory_limit to 128MB. I can adjust the limit to 64M but never above the limit of 128M.

    Did someone noticed a similar problem?

  8. I tried to install wordpress 3.0.1 and i received this message : Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 2966269 bytes) in /home/blidaru/public_html/wp-includes/http.php on line 1364

    What i have to do?

    • put this on top of your http.php file

      ini_set('memory_limit','32M');

      if that didn’t worked out try increasing 32M until it works for you

  9. ini_set('memory_limit', '-1'); will be a neat solution as well. but I am still not sure of any side effects of this!

    • Chasalin

      Dangerous!

      If you happen to have any flaw in your code, this could cause your server to become unresponsive.

      Setting the memory_limit to -1 (infinite) should only be done for testing purposes on non-production machines!

  10. Life saver, as always David.

    Host of my dedicated saw fit to lower all max memory across their entire network for “security reasons”. Screw em, Big Daddy Walsh has my answer!

    • Blgr3

      Tried, didn’t work…

  11. Thanks David but it didn’t work in my case – I think my host has put a limit on memory access – had an image resizing script with imagecreatetruecolor() – and script just would not go past it.

  12. gaurav

    set

    ini_set(“memory_limit”,”128M”);

  13. berk

    Here are 3 ways on how to increase it

    http://newexception.com/php-increase-memory-limit

  14. I need to put this in php.ini file or .htaccess file ?

  15. @samily .htaccess is important in main folder (public_html) not in php.ini

  16. Sanjiv

    Great works I just added and it works.

    ini_set('memory_limit','32M');
  17. Egon_Freeman

    Just so you know, the administrator CAN limit your ability to increase this limit! There’s such a thing called PHP Safe Mode, and in this mode you can set function calls that will NOT be executed, no matter who calls them or from where. One of these functions can be ini_set(), which would block it completely.

    You can also try setting it via .htaccess, but that doesn’t always work (it hinges on ALLOW OVERRIDES being set to something other than NONE, I believe).

    Keep that in mind. Sometimes, the only valid solution is to change your host.

  18. Hi David,

    I’m trying this right now. Somehow from php.ini it’s already 512M. In wp_config.php I also put define('WP_MEMORY_LIMIT', '512M'); but somehow it keep getting 256M.

    I decided to use the ini_set('memory_limit','1024M'); in my php file to see if it has any improvement. So far it’s not breaking due to memory limit yet.

    So thank very much for pointing this out.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • Loading Static Templates for Intern Testing

    I use Intern by SitePen for all of my JavaScript functional testing.  Intern has loads of features other functional test frameworks don't and it's completely Promise-based -- something I got very used to when I used the Dojo Toolkit every day. Async test creation can...

  • Convert Video to mp3

    Let's all be honest for a moment:  we've all ... not paid for ... music.  Whether it was via a file sharing app like Kazaa or Napster, or it was downloading and seeding on bittorrent, or maybe even downloading a music video and ripping its audio,...

  • Sort git Branches by Date

    I'll be first person to admit I don't do as much git repository maintenance as I should.  I rarely delete branches which have been merged, so a git branch execution shows me a mile-long list of branches that likely aren't relevant.  The best way to find branches I've recently...

  • Best Tools and Resources for Web Professionals in 2015

    Looking for the right resources to help you satisfy the needs of your clients? On the lookout for the best tools to help you increase your revenue? Searching for the right software to help you improve your business? Well, then you’ve come to the right place....

  • JavaScript Polling

    Polling with JavaScript is one of those ugly but important functions within advanced front-end user experience and testing practices.  Sometimes there isn't the event you can hook into to signify that a given task is complete, so you need to get your hands dirty and simply poll for...