Sorry in advance for the newbie question, but I have a dedicated virtual server I'm working on that has 512MB of RAM memory. I've recently changed the memory_limit from the default of 128M to 256M. Is there any reason why I wouldn't want to bump this all the way up to the full 512M that's available?


Is there any reason why I wouldn't want to bump this all the way up to the full 512M that's available?

Yes - the first is that's 512MB per PHP process. Do your processes really need that much?

Some reasons I can come up with that haven't already been covered:

  1. Setting a limit that high will only encourage clumsy programming (I can't imagine PHP would be the best language to pick if you need half a gig of ram in one shot)

  2. Potentially denial of service attacks (e.g. if you do upload processing in memory, and the user knows that, they'll just upload some really large files and knock your server offline - rather than you server hitting 32/64/128M, it'll hit all the way to 512M). This is assuming you don't have other upload limits that aren't hit first.

  3. You have a virtual server. If it's OpenVZ/Virtuozzo and you can't burst to more than 512M, then your processes will start to get killed - fast. Apache will probably be knocked offline, especially when using dso. This will put everything down until it's restarted.

  4. You won't know if this'll cause a major problem until one starts to surface. Unlike more complex memory management, PHP makes no attempt to allocate this much memory at the start. It simply says it'll keep trying until you try to use more than this, then give up. For all you know, you might not be able to get anywhere near 512MB in the first place - so you can't assume just because you've restarted Apache and everything still works, that you are fine.

  5. Suppose you do have an application that requires 512M of memory. Why make that limit for all other processes too? You risk creating the problems above for all of them. With a standard installation, the script that requires it (e.g. a large cron job) can just do ini_set('memory_limit','512M'); in PHP and raise it's own limit. That leaves all other processes 'safe'

Here are some more lower-level reasons:

  1. Your entire virtual server is allocated 512M. That has to be divided into processes just to keep the server running, such as (at a minimum)

    • Cron
    • Apache
    • MySQL or some database server

    It's not possible to squeeze all of these other processes and a 512M PHP process, within your total 512M memory.

  2. On top of this, the kernel will optimise unused ram into a cache - so carelessly having a high limit risks a performance penalty for no real reason.

  3. Confirm if you have swap (verify using top or free -m). If you do, your virtual server will slow dramatically when you hit the limit. If you don't have swap, your processes will be killed by the hypervisor. Either way, you're going to see a performance hit.

So, in summary, I wouldn't advise raising a global limit for something like this.

  • ...and even on a Virtual server, some of the memory is needed for I/O caching. – symcbean Jul 8 '12 at 21:06
  • Agreed, but I avoided OS fundamentals as it's been mentioned in the other two answers (so I didn't want to repeat them) – Jay Jul 8 '12 at 21:10
  • Feel free to add the O/S Fundamentals. My answer was short since the question's preposterous. The information you provided is useful and might make something out of this sow's ear after all. – Magellan Jul 8 '12 at 23:55
  • Setting too high a memory limit will also suppress the intrinsic garbage collection in PHP causing more problems. – symcbean Jul 9 '12 at 15:51
  • Interesting, do you have a link? I had a browse through PHP 5.3's GC but didn't see any mentions of memory_limit – Jay Jul 9 '12 at 15:58

I'm sure you can set it to that. It's probably not advisable since your kernel and other processes need some memory too. Depending on what kind of storage your VM is hooked up to, your system may benefit from caching its storage in memory as well


The operating system that runs your application will always need memory. If you raise it over and beyond what is available, you could find you app swaps to disk more that you were expecting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.