Setting memory_limit in php.ini – can I use all of the available system memory

memoryphp.ini

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?

Best Answer

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.

Related Topic