Question

When performance is important including server memory,I am curious if using output buffering like ob_start(); in PHP has ANY performance hits over not using it? Does it use more memory or anything to use it?

In my situation on a high traffic site where I need all the memory I can for memcache and APC and all the other server activities I am just curious if I should use it or not, the only real reason it comes in handy for me is for redirecting pages , sending headers I should say after a header has already been sent, my site has header , body, footer file setup so sometime I need to redirect depending on what is in the body file so if the header is already shown ion screen that creates a problem, using output buffering is 1 solution but there are other solutions so just curious about performance

Was it helpful?

Solution

I think it's best to use it with a high traffic site, or at least turn implicit flush off, to avoid sending partial responses over network, because it can slow down the rest of the script if the receiver is very slow too.

By sending the whole response in one time, you free all resources used by the php script, so it's more efficient.

OTHER TIPS

There are two reasons why output buffering is useful

  1. For performance so you're not waiting for the network socket to be available each time you echo.
  2. To avoid sending the headers too early. Once you've sent some content to the browser the headers must also be sent, after this is done then you cannot modify them e.g. if you want to set a cookie or change the content-type.

There is of course the penalty of storing everything in memory till the end of the request. This should, ordinarily, be quite small compared to the overall size of the PHP process. That is, unless, you plan on sending a massive file down the wire. If that's the case you can periodically flush the buffer using ob_flush() and flush() (or temporarily disable the buffer altogether) to reduce the peak memory used.

In my opinion you should have it on all the time and remove it in exceptional cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top