Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does output buffering in PHP require more resources?

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

like image 367
JasonDavis Avatar asked Oct 01 '09 09:10

JasonDavis


People also ask

What is output buffering in PHP?

Output Buffering is a method to tell the PHP engine to hold the output data before sending it to the browser.

What is the purpose of an output buffer?

An output buffer is a location in memory or cache where data ready to be seen is held until the display device is ready.

How stop PHP buffering?

You can turn on output buffering with ob_start() , and turn it off with ob_end_flush() or ob_end_clean() . You can also have all your scripts automatically start with output buffering on using the output_buffering option in php.

How do I enable output buffering?

You can enable output buffering for all files by setting this directive to 'On'. If you wish to limit the size of the buffer to a certain size - you can use a maximum number of bytes instead of 'On', as a value for this directive (e.g., output_buffering=4096). This directive is always Off in PHP-CLI.


2 Answers

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.

like image 185
Neel Avatar answered Oct 31 '22 13:10

Neel


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.

like image 3
Julien Avatar answered Oct 31 '22 11:10

Julien